0

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

2
  • Are you sure reverse_data[(max_number_samples-1)-i] is correct? You are writing data only to the end part of the reverse_data array, but than you read them from the beginning. Commented May 10, 2012 at 15:07
  • ah, I didn't see this comment. You are correct, but then I used another loop to search for the start of the audio, and only write the data from the start of the audio to the end. Thanks a lot for your help! Commented May 10, 2012 at 15:34

2 Answers 2

6

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]);
}
Sign up to request clarification or add additional context in comments.

8 Comments

+1 ...and do not use recursion for such large amount of data!
Ah thanks, so the issue lies in the size of the arrays, as i expected! How would I initialise my arrays on heap, rather than stack?
Re: "allocate only as much as you need": If max_number_samples if bigger than track_samples, then reverse_data[(max_number_samples-1)-i] will crash when i==0.
@Rob: I assume it's a mistake in the OP's code: he wanted actually track_samples instead of max_number_samples.
|
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.