Memory allocation in C++

 Here your will understand C++ memory allocation and how to allocate dynamic memory,

Memory allocation in C++

we declare the variable in the program to store the user input. these input take the space in the memory according to its data type at compile time by default but using dynamic memory allocation in C++ these input take the memory at run-time.

The memory taken by the variable remains in the program until the execution of the program terminates, and since they allocate memory at compile time we cannot change their size at run-time, means their size remains fixed.

using dynamic memory allocation in C++, we can reserve the memory as per our requirement (up to a limit) at run-time and free it when the need ends.

Let us understand this a little more easily

 We have made a program. where two functions are used, the first function has variable x declare and in the second function any two variables a and b have been declared. When the execution of the program is executed then after the execution terminate in the first function, the execution is transferred to the next function but here the first function is terminated and now we do not even need it but still in that function the declare variable x has memory is reserved and this memory will be free only when the program will terminate.

Type of Memory allocation in C++

Memory allocation or management is the most important and powerful concept of C++. There are two types of memory allocation in C ++.

Static Memory allocation in C++

This is allocated / reserved in memory allocation compile time. Therefore it is also called compile time memory allocation. Because the memory size is fixed in that it is called static memory. The memory allocated by static memory allocation is de-located or free only when the program is terminated.

It has no syntex. Meaning when we declare any variables or objects in the program. So its allocate memory in compile time automatically. So in a way, the general declaration of variables or objects is an example of static memory allocation. For example-

int a; char; int arr[5];

In static memory allocation, memory is automatic allocate (at compile time) and automatically de-locate (only when program is terminated). So far the programs given in this tutorial are examples of static memory allocation.

Dynamic Memory allocation

allocate memory on run-time is called dynamic memory allocation. In dynamic memory allocation we can change the memory size at ru-ntime and free it when the requirement is end.

Note here that if we allocate memory using dynamic memory allocation, we have to manually de-locate it here as well, in case of not doing so, there may be a memory leakage in the program.

such situation does not arise in static memory allocation because there the memory de-location is automatic.

Because the memory allocation in dynamic memory allocation is at run-time, when the memory size is not detected at compile time, that is, at run-time it depends on the need of the user that how much memory to allocate.

Here is the example where we allocate dynamic memory in array.

allocating dynamic memory in C++

In C++, the new operator is used for dynamic memory allocation. in this memory allocation (by new operator) and de-location (by delete operator) both are manually, by the programmer. Because in this, we allocate memory as per requirement at run time when needed, hence Dynamic memory allocation program provides flexibility.


data-type pointer-variable = new data-type; 

The pointer-variable is used to hold the address of dynamically allocate memory, this dynamically created memory is accessed from this pointer-variable itself. Here the data-type can be built-in-data or user-defined-data-type. means data-type int, char, array, structure or class type here.

new operator

In the dynamic memory allocation, firstly we have to declare a pointer variable, which hold the address of dynamically allocated memory. Note that the size of the memory allocation will depend on the data type. For example-

int *p;
new int; // 2 byte( because int data type take 2 byte)\

Then assign new int address into pointer variable *p

p = new int;

Then store value using pointer-variable *p

*p = 5; 

We can also declare it in a single statement as if we want to-

int *p = new int(5);

delete operator

as mentioned above, it is mandatory to de-locate/release dynamically allocated memory. The delete operator is used to release dynamically allocated memory. Its syntex has been given below.


delete pointer-variable;

Here, pointer-variable will only have the name of pointer-variable i.e. in memory de-location asterisk (*) will not be used.

Let’s try with an example

The program of memory allocation of a simple pointer variable is given below. In which memory is allocated by new-operator while memory is de-located by delete-operator.

allocate dynamic memory for int data type


 void main()
   int *p = new int; // memory allocated at runtime

   *p = 5; //assign value into *p

    cout<<*p; //print

    delete p; // memory release



The program allocates a dynamically memory of int type from the new keyword whose address is stored in a pointer variable p* and a value 5 is stored in that memory in the next statement. Store this value in memory is printed by pointer variable p*. After the result is printed, dynamically allocated memory is also released by the delete keyword.

Note here that dynamically will be allocated memory by 2 byte because we have allocated the memory of int data type whose memory size is 2 byte.

Free store or heap

Heap is an unused memory space given to the program. For dynamic memory allocation, we use this same memory. But sometimes it can happen that heap does not have sufficient memory space for dynamic memory allocation. This can happen by allocating multiple time dynamic memory. So it is a good option to check sufficient memory in heap before dynamically allocating memory in a program. An example of this is given below.

int *ptr=new int;
if(!ptr) //ptr==NULL
   cout<<"Can't allocate Memory..!";

To keep its memory, we de-locate the dynamically allocated memory. So that during execution we can use it for someone else.

Things to know

Many times during the program execution, we do not know how much memory we need (such as in an array) and can not declare any variable at execution time which can meet the need of memory. In such a situation, we can fulfill this need by using dynamic memory allocation.

as it has been said, dynamic memory allocation occurs from the heap which is unused by the program memory. Memory wastage in a way So this is a better way to use this memory.

Although we can also use calloc (), malloc () from which dynamic memory is allocated in C language, in C ++ but in constructor in OOPs, in these function constructor, object cannot be initialized. Meaning new operator is used for dynamic memory allocation in OOPs.

When we allocate dynamic memory from calloc(), malloc(), it will be de-locate with the same function free(). That is, we can not release allocated memory from calloc() or malloc() by the delete operator. Yes, it may be possible somewhere in the GCC compiler (GNU Compiler Collection). But normally delete operator is used with new.

more about dynamic memory allocation,

previous-Pointer in C++

next- Function and their type | Parameter type