Memory Management in C++

What is Memory Allocation?

Two ways in which memory can be allocated for storing data are:

  1. Compile-time allocation or Static allocation of memory: Here the compiler allocates memory for the named variable. The exact size of the variable must be known at compile-time.
  2. Runtime allocation or dynamic allocation of memory: Here the memory is allocated during program runtime and the allocation of storage space is done dynamically and the memory segment is known as a heap. In this case, the specific space or number of the item does not have to be known by the compiler in advance. Pointers have a significant role in this case.

What is Dynamic Memory allocation?

Many times, we are not aware in advance how much memory will be required to store a piece of specific information in a defined variable and the size of the required memory space can be determined at run time. C++ Programmers can dynamically allocate memory space while the program is running, but cannot create new variable names at run time, and for this reason, dynamic allocation needs two criteria:

  • Creating the dynamic space in the memory storage
  • Holding its address in a pointer

What are applications?

  • Allocate memory of variable size at run-time which is not possible with compiler allocated memory except variable-length arrays.
  • Increases flexibility for the programmers. Programmers are free to allocate and deallocate memory as required.

Memory in your C++ program is divided into two parts −

  • The stack − The variables declared inside the function will take up memory space from the stack.
  • The heap − This is the unused memory space of the program and can be used to allocate the memory dynamically during the program runtime.

Memory can be allocated at run time within the heap for the variable using a special operator in C++ programming language which returns the address of the space allocated. This operator is known as the new operator.

If not required the dynamically allocated memory can be removed using the delete operator, which de-allocates memory space that was previously allocated by the new operator.

new operator

Syntax to use new operator:

new data-type;

The new operator denotes a request* for memory allocation. Only if sufficient memory space is available, the new operator initializes the memory storage space and returns the address of the newly allocated space, and then initializes memory to a pointer variable.

*What if enough memory is not available during runtime?
If enough memory space is not available in the heap for allocation, the new request will fail and throw an exception of type std::bad_alloc ( “nothrow” can be used with the new operator, which will return a NULL pointer.

int *x = new(nothrow) int;
if (!x)
{
   cout << "Failure in memory allocation, Out of Space\n";
}

Normal Array Declaration vs Using the new operator
Normal arrays are deallocated by the compiler. However, dynamically allocated arrays always remain there until either they are deallocated by the programmer or the program terminates.

delete operator

It is the programmer’s responsibility to deallocate and remove dynamically allocated memory space, for this purpose the programmers are provided with the delete operator.
Syntax:


delete pointer-variable;  // Release memory pointed by pointer-variable

Malloc() in C vs new Operator in C++

*C++ also supports malloc()

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?