C++ Programming/Memory Management Techniques< C++ Programming
You can allocate/deallocate each object as you require, or you can recycle your objects.
This would be the most basic approach. You allocate memory using the 'new' operator. You deallocate using the 'delete' operator.
In addition, C-style memory management functions like malloc, realloc, free etc. can be used for allocating/deallocating memory. Using 'new' would be preferred in most cases.
Have a look at auto-pointers later in this article, to facilitate automatic deallocation of pointers.
If you are going to allocate and deallocate repeatedly, it might be advisable to use memory pools.
Using memory pools has several advantages.
- You can control the amount of memory you want to allocate. For example, your memory pool helper functions, or memory pool classes can limit the amount of objects to be allocated. They might return NULL above a limit. Alternatively, in multi-threaded applications, they can choose to wait till a consumer has 'returned' some objects.
- If you allocate in a single block, you get contiguous memory, compared to allocating one by one. Contiguous memory can be faster in terms of cache hits.
- You have much more control over allocations and deallocations. For example, the memory pool can keep track of all the memory 'loaned' to other classes. It can provide a functionality whereby all the memory can be freed by a single function call. This can be helpful if you don't want to worry about deallocating individual objects, and instead want to deallocate all used objects in a single call at the end of a function etc.
- Your memory pool classes can behave dynamically based on the configuration of the system. For example, in a multi-threaded consumer-producer model, your memory pool classes can choose an appropriate cache size based on the available memory in the system.
You need to manage pointers in C++. Here are a few tips:
- As a general rule, each pointer should be pointing to a valid memory location, or should be NULL. You should avoid dangling pointers at all costs.
- As far as possible, you should not have more than one pointers pointing to the same memory. This could lead to dangling pointers, in case you free the memory using one pointer but don't set the other pointer(s) to NULL.
- You should try to create and destroy the pointers in the same location. This makes it easy to keep track of what you are allocating and freeing.
- Never double-free a pointer. And never reallocate a pointer to a new memory location if it was already pointing to a valid memory location, unless of course, you have some other way of freeing the old memory location.
- Each memory location that you allocate using 'new' or 'malloc' etc. should be freed once you are done using the memory. Not freeing this memory would be called a memory leak. In general, it is a good idea to prevent memory leaks even if you know that you are not going to require too many allocations and your program is going to end soon.
(The book Memory Management goes into far more detail).
Auto pointers save you the task of freeing memory once you are done using a memory location. Auto pointers are part of the C++ standard library.