Question

I have a relatively simple question. In one of my CS classes, for an assignment, we have to make a simple side-scrolling game using C++ and the XLib libraries. In a forum we have for the class, a lot of students complained about memory leaks and issues with dynamic allocation. I am almost done the assignment, but I haven't had to use any dynamic allocation. I've just been using stack space, and have had no issues with this so far.

I am just wondering if there is any drawbacks to this? Seems like a lot of the other students are using dynamic allocation. If my little game works fine just using the stack, do I have any reason to worry?

Thanks guys.

Was it helpful?

Solution

There is nothing wrong per-se about using memory on the stack, though you need to be careful about allocating anything of decent size.

For instance:

// inside some function ...
int blah[1000];
// ...code using blah

might work fine, or you could run out of space and fail silently.

I would suggest using new / malloc() if you need a big chunk of memory, but it's fine to use small arrays on the stack.

OTHER TIPS

If you're so worried about dynamic memory allocation and memory leaks, why not try use a Smart Pointer:

Here http://en.wikipedia.org/wiki/Smart_pointer#C.2B.2B_smart_pointers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top