title: Dynamic Memory Allocation
Dynamic Memory Allocation in C++
What is Dynamic Memory Allocation in C++?
- Memory Allocation in C++ refers to the memory alloted to the variables you use throughout your program.
- Dynamic Memory Allocation in C++ refers to the memory which is alloted to the variables at run-time, which is also when the amount of memory required is decided.
- This memory comes from the heap, whereas non-static variables and local variables get memory from the stack.
- In C++, the programmer can manually perform dynamic memory allocation as described below.
- It is possible in C to do dynamic memory allocation by using the calloc and malloc functions to allocate memory as needed, and then using the free function to deallocate it.
- In C++, in addition to the above C functions, there are two operators, new and delete, for respectively performing dynamic memory allocation and deallocation.
newoperator can grant the programmer memory from the heap (if available). If the memory which the programmer asks for is available, then the
newoperator initializes the memory and returns the address (reference) of the memory allocated (otherwise an exception of type
int *ptr= new
int *ptr2= new
pointer-variable-typeis a pointer of
data type. The
data-typecan be either a primitive (int, char, etc.) or a user-defined data-type.
- In C++,
newis preferred over
newworks for objects by calling their constructors. For example, if an object
Foohas both a default constructor and a constructor that takes an
intargument, Example 3 below will construct a
Fooobject using the former constructor, while (if
ais of type
int), Example 4 will construct a
Fooobject using the latter constructor instead.
Foo *ptr= new
Foo *ptr= new
- In C++, it is programmer’s responsibility to deallocate dynamically allocated memory, since otherwise the memory will not be available to be reused after the end of the program.
- To deallocate the memory, the
deleteoperator is available and can be used by the programmer as follows.
For example, to free the memory allocated in Examples 1, 3, and 4 above, we type:
In Example 2 above, however, memory for an array of integers was allocated. To free the memory alloted for an array, the
delete operator must be used:
delete  ptr2; Memory Leaks Memory leaks are caused when you fail to deallocate the dynamic memory you allocated via the
newoperator by the end of your program. If you do not deallocate this memory with the
deleteoperator, it will accumulate in the heap every time the program runs. This causes your computer to slow down because memory is not deleted and your available memory decreases. Many poorly written computer programs exhibit memory leaks over time. However, when you reboot the machine, it will resolve the issue, as all memory in the heap will be released at that point. There are various ways to check for memory leaks in your program. Valgrind is a common tool for doing this from a bash linux shell. Once valgrind is downloaded and the program is compiled, valgrind can be run with certain flags for displaying different information about memory read and write errors and memory leaks. Here is an example of running valgrind with a compiled program ‘a.out’:
Mechanism Memory allocated/deallocated in C++
C uses malloc() and calloc() function to allocate memory dynamically at run time and uses free() function to free dynamically allocated memory. C++ supports these functions and also has two operators new and delete that perform the task of allocating and freeing the memory in a better and easier way.