Memory Allocation
Explore explicit memory allocation in C++ including dynamic allocation with new and new[], object creation using placement new, and handling allocation failures. Understand techniques to optimize memory management for safer, efficient C++ programs.
We'll cover the following...
Introduction #
Explicit memory management in C++ has a high complexity but also provides us with great functionality. Sadly, this special domain of C++ is not so well known.
For example, we can directly create objects in static memory, in a reserved area, or even in a memory pool. This functionality is often key in safety-critical applications in the embedded world.
-
C++ enables the dynamic allocation and deallocation of memory.
-
Dynamic memory, or the heap, has to be explicitly requested and released by the programmer.
-
We can use the operators
newandnew[]to allocate memory and the operatorsdeleteanddelete[]to deallocate memory. -
The compiler manages its memory automatically on the stack.
...