0

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.

Tesla
  • 822
  • 2
  • 13
  • 27

2 Answers2

1

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.

Andrew Pouliot
  • 5,423
  • 1
  • 30
  • 34
0

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