Running Out of Stack Memory?

Started by
5 comments, last by Chryzmo 18 years, 9 months ago
Heyas, I am trying to write a 2D game in C++ using the SDL library. As of now, I have an array of 16 SDL_Surfaces to hold my tiles and I created a char[16][16] to hold the text representation of the map. When I try to run my program, I get an SDL Segmentation fault. However, if I comment out the line that creates the char[16][16], the program compiles and runs fine. Am I trying to create too many things on the stack and I need to start using the heap? I have yet to actually start using the new and delete keywords because I guess I've never reached the point where I actually needed to. Again, I'm not actually sure that this is the problem, but I would think SDL_Surfaces would require more memory than other types of objects because they hold graphic data rather than just text or numbers. Is there anyway to determine if this is infact the issue? Thanks, Chris
Advertisement
It's probably a buffer overrun that just happens to only overflow into non-committed memory because the char[16][16] happens to bring the stack address to a page boundary. But that's just a guess.

The default size of stack is usually about a meg in memory. Do you think you're using that much memory on the stack?
I don't think I am using 1MB of memory. The GIF file that I have the tileset saved in is only 1KB, then I splite that into 16 seperate surfaces. So, I would think I am only using 2KB at most, plus a few other variables that should come no where near 1MB in total.

That would mean there is something wrong inside my code that is creating the buffer over flow, right?

(edit) I just looked back through everything and I haven't even touched the char array yet. The only thing I did was initialize it as a member of a class. If I change it to char cMap[11][11]. The program still runs, but if I create it any larger than that I get the SDL Segmentation fault. The exact error is:

Fatal signal: Segmentation Fault (SDL Parachute Deployed)

However, I really don't think there is anyway I have used anywhere near 1MB of memory.
hmm, i didn't know you could run out of memory (other than the total your comptuer has of course), how do you access this "heap" when you run out of "stack" memory?
Charles Reed, CEO of CJWR Software LLC
Quote:hmm, i didn't know you could run out of memory (other than the total your comptuer has of course), how do you access this "heap" when you run out of "stack" memory?

You start using pointers.
int *i = new int;...delete i;

Like that. :)
.gif files are compressed (although loss-less, so no quality degradation) so the file size may be completely different from the size in memory. Usually when loaded you'll be converting into full 32 or 16bit colour (rather than gif's 256 colour pallete format) and be uncompressed.
I got the program to run by dynamically allocating the char array. I guess it did have something to do with a lack of stack memory.

Now, I want to avoid memory leaks and I've never used dynamic allocation before. The char array is a member of one of my classes and I deallocate the array in the destructor but when I call the class I just call it normally on the stack, not as a pointer. So, is the destructor called when the class falls out of scope? And what if I end the program while it is still running in the loop? Say it is doing it's thing and I just close out of it(click the X to close the window). Is that going to leave the memory of the array still allocated, resulting in a memory leak?

Thanks,
Chris

This topic is closed to new replies.

Advertisement