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 functionality to allow for larger arrays (up to 5 minutes of audio, 26460000 pieces of 16bit data ~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];
}
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).
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
: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:(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
i
th and(size - 1 - i)
th items: