堆栈溢出视觉 C++,可能是数组大小?
据我所知,这不是无限递归造成的.
As far as I know, this isn't caused by an infinite recursion.
程序在较小的数组中正常运行(它是一个音频编辑器).现在我增加了功能以允许更大的数组(最多 5 分钟的音频,26460000 条 16 位数据 ~50mb).
The program functioned correctly with smaller arrays (it is an audio editor). Now I have increased functionality to allow for larger arrays (up to 5 minutes of audio, 26460000 pieces of 16bit data ~50mb).
由于增加了数组的大小,我在一个特定函数上收到堆栈溢出错误,它应该通过将数组向后写入新数组来反转输入文件的播放,然后覆盖原始数组.我猜每个数组可能高达 50MB,这可能是问题所在:
Since increasing the array's size I am receiving stack overflow errors on one particular function, where it should reverse the playback of an input file by writing the array into a new array backwards, then overwriting the original array. I'm guessing as each array could be up to 50MB this may be where the problem lies:
//initialise temporary new array to place samples in
short signed int reverse_data[max_number_samples];
for (i=0; i<track_samples; i++)
{ //puts data from sound_data into reverse_data backwards.
reverse_data[(max_number_samples-1)-i]=sound_data[i];
}
for (i=0; i<track_samples; i++)
{ //now overwrites sound_data with the data in reverse_data
sound_data[i]=reverse_data[i];
}
我对 C++ 和一般编程相当陌生,我不确定我在调试期间遇到的错误是否真正告诉了我.
I'm fairly new to C++, and programming in general, and am unsure of what the errors I get during debug are really telling me.
任何帮助将不胜感激,我确信有一个足够简单的解决方案(我读过涉及堆"的内容,但我不确定堆"到底是什么).
Any help would be appreciated, I'm sure there's a simple enough solution (I've read stuff involving 'heaps' but I'm not confident of what a'heap' really is).
推荐答案
正如 Vlad 指出,不要分配堆栈上有 50MB.
As Vlad pointed out, don't allocate 50MB on the stack.
但是,这一点没有实际意义,因为您不需要分配 任何 数据.尝试通过一次调用 std::reverse来替换整个代码片段代码>:
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]);
后记:别忘了
#include <algorithm>
.
相关文章