Stack overflow visual C++, potentially array size?

后端 未结 2 1080
失恋的感觉
失恋的感觉 2020-12-12 05:24

As far as I know, this isn\'t caused by an infinite recursion.

The program functioned correctly with smaller arrays (it is an audio editor). Now I have increased fun

相关标签:
2条回答
  • 2020-12-12 06:03

    You should not allocate large data structures on stack, because the stack's size is bounded. Allocate it on heap.

    Even better, you should avoid manual allocation and use std::vector, which will care for the memory allocation itself. As a bonus, you won't need to care about deallocation. (And this is the modern C++ way.)

    By the way, if max_number_samples is big, you should perhaps allocate only as much as you need:

    std::vector<short int> reverse_data(track_samples);
    

    (the rest of your code stays as it is).

    Edit:
    Even better idea: you can reverse your array in place, without copying into an additional array! Just go from index 0 to the half size and swap the ith and (size - 1 - i)th items:

    for (i=0; i < track_samples/2; i++)
    {
        std::swap(sound_data[i], sound_data[track_samples-1-i]);
    }
    
    0 讨论(0)
  • 2020-12-12 06:11

    As Vlad pointed out, don't allocate 50MB on the stack.

    But, the point is moot because you don't need to allocate any data. Try replacing your entire code fragment with a single call to std::reverse:

    std::reverse(&sound_data[0], &sound_data[track_samples]);
    


    Postscript: Don't forget to #include <algorithm>.

    0 讨论(0)
提交回复
热议问题