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.
NEW operator
- The
new
operator can grant the programmer memory from the heap (if available). If the memory which the programmer asks for is available, then thenew
operator initializes the memory and returns the address (reference) of the memory allocated (otherwise an exception of typestd::bad_alloc
is thrown). - Syntax
pointer-variable-type
= newdata-type;
Example 1:int *ptr
= newint;
Example 2:int *ptr2
= newint[10];
Here,pointer-variable-type
is a pointer ofdata type
. Thedata-type
can be either a primitive (int, char, etc.) or a user-defined data-type. - In C++,
new
is preferred overmalloc()
becausenew
works for objects by calling their constructors. For example, if an objectFoo
has both a default constructor and a constructor that takes anint
argument, Example 3 below will construct aFoo
object using the former constructor, while (ifa
is of typeint
), Example 4 will construct aFoo
object using the latter constructor instead.
Example 3:Foo *ptr
= newFoo;
Example 4:Foo *ptr
= newFoo(a);
DELETE operator
- 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
delete
operator is available and can be used by the programmer as follows. - Syntax
deletepointer-type-variable;
For example, to free the memory allocated in Examples 1, 3, and 4 above, we type:delete ptr;
In Example 2 above, however, memory for an array of integers was allocated. To free the memory alloted for an array, thedelete []
operator must be used:delete [] ptr2
; Memory Leaks Memory leaks are caused when you fail to deallocate the dynamic memory you allocated via thenew
operator by the end of your program. If you do not deallocate this memory with thedelete
operator, 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’:valgrind ./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.