Sign in to follow this  
ciranmc

The Heap in C++

Recommended Posts

I'm currently studying all about heap memory. I understand things like creating and deleting memory on the heap and clearing up dangling pointers. However I am not sure how working with the heap allows me to write better games. Any help would be appreciated.

Share this post


Link to post
Share on other sites
First, a word about terminology: the heap is the memory space manipulated by the functions malloc, calloc, realloc, free and their friends from the C standard library. They exist in C++ for backwards compatibility reasons with C89 programs, but in practice are not used because they do not "fit in" with the C++ philosophy.

The new operator in C++, which is used for performing memory allocation by hand (something not done very often) works on the free store, a memory space reserved to the C++ language. If you're using C++ and want to allocate memory dynamically, you should be using the free store, and not the heap.

Now, here's the trick: in C++, creating an object is done in three different ways. Either you 1° create a variable of the object's type (at which point, the object dies when the variable goes out of scope); or 2° you create a member of the object's type in another object, either one of your own, or a standard library container (so the object dies when the object who contains it dies); or 3° you allocate the object on the free store using new (at which point the object dies when you delete it).

Techniques 1° and 2° are sufficient for 99% of all cases you might encounter. The only situation where 3° is useful is when you wish to return a new object (and cannot afford to return it by value so that it gets copied), at which point the typical approach is to create the object dynamically so that it can outlive the function it was born in (something not possible with techniques 1° and 2°) and return a pointer or reference to it.

I personally advise returning either an std::auto_ptr (the default choice when returning pointers, because it prevents memory leaks) or boost::shared_ptr (if you know that your object will be kept around and shared by many users).

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