Sign in to follow this  
Chryzmo

Running Out of Stack Memory?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
.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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this