Stack overflow visual C++, potentially array size?

笑着哭i 提交于 2019-11-28 14:41:08
Robᵩ

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>.

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]);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!