Sign in to follow this  
Uphoreum

When to Use the Heap

Recommended Posts

I understand the difference between the stack and the heap, however I'm not sure when I should dynamically allocate memory on the heap, and when I should use the stack. There are certain times when I can see that dynamic memory is required, but a lot of the time it seems you could go either way. For example, I have my main class, which contains the game loop, and basically encapsulates everything else. In my main function, I create an instance of this class, but I'm wondering: Should I dynamically allocate this with 'new' or should I just put it on the stack? Same question goes for all the members of that class. I have a Window class, a Keyboard class, a Renderer class, etc within the main game class. Should those be pointers/allocated or should they just be on the stack? Thanks!

Share this post


Link to post
Share on other sites
It depends on the platform you're working on (i.e.: how much stack space is available, in what kind of memory is it stored, what does you allocator do with small allocations, etc.).

In general it is faster (and easier) to use the stack when possible. Use the heap for allocations that are really dynamic (i.e.: you only know the allocation size at run-time).

Share this post


Link to post
Share on other sites
Look up RAII.

Or look at what the standard library does.
std::vector is a good example. The vector object is on the stack, because that means you don't have to remember to free it. Internally, it allocates memory on the heap, and frees it in the destructor, which is guaranteed to be called when the object goes out of scope.

Simple as that. No memory leaks or anything.

If you have a smallish fixed-size object, put it on the stack. If the object has a dynamic size (like the vector), then obviously some part of it has to be heap-allocated (really big objects should be allocated on the heap too, because you don't have that much space on the stack typically). Then hide the heap allocation and subsequent freeing of memory inside a (stack-allocated) wrapper object.

That's the general rule. And when you *do* have pointers, wrap them in smart pointers which use the same principle as above to ensure the pointer gets freed.

Share this post


Link to post
Share on other sites
The point of using tools like std::vector and techniques like RAII is to be able to treat your heap allocations, basically, as if they were stack allocations. This makes things a lot easier overall, and improves the organization of your code.

Once you're doing this throughout, it's no longer a question of "do I need to dynamically allocate this?" as of "do I gain anything from using std::vector here?", or things of that sort.

But in general, do it the simple way unless you see a reason why it wouldn't work.

Share this post


Link to post
Share on other sites
Quote:
Original post by Uphoreum
I understand the difference between the stack and the heap, however I'm not sure when I should dynamically allocate memory on the heap, and when I should use the stack.

There are certain times when I can see that dynamic memory is required, but a lot of the time it seems you could go either way.
Thanks!


I understand cars I drive them every day, but I not sure when I should leave the door open and when I should close it. There are certain times when I can see having the door open is required but a lot of the time it seems like it could go either way.

If you don't understand feel free to say I am not quite getting it. Though I must say you ask better questions than most.

You should pretty much always use the stack. If you have an object who's lifetime doesn't neatly fit in the scope of a function call use the heap to control it dynamically. If you don't know(at compile time) how many of an item there are going to be use the heap(or better yet let a vector use the heap for you). Finally if you need polymorphic behavior use the heap.

Now specifically to the items you mention, use the stack for all of them unless you need polymorphic behavior. The main game class will control their lifetime nicely.

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