memory allocation in C++ with examples

Here you will understand C++ memory allocation and allocate memory,

Memory allocation in C++

We declare variables in the program to store user input. These inputs reserve the place in memory according to their data type at compile time by default, but using dynamic memory allocation, we can re-serve the memory of these inputs 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.
  • dynamic memory allocation gives us a facility wherein we can reserve the memory as per our requirement (up to a limit) at run-time and free it when the need ends.

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
  • Dynamic memory allocation

static memory allocation in C++

  • The static memory allocation is by default.
  • In this, the memory allocation is at compile time. Therefore it is also called compile time memory allocation.
  • Because the memory size is fixed in it, it is also called static memory allocation.
  • The memory allocated by static memory allocation is de -locate or free when the program terminates.

It has no syntax. meaning when we declare any variables or objects in the program. so it allocate memory 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];

These data types will take up memory as Follows ,

a = 2 bytes;
b = 1 byte

because int memory size is 2 bytes and char 1 byte. while in Array,

arr[5] = 10 bytes // 2 byte x 10 = 10
name[10] = 10 bytes // 1 byte x 10 = 10

So now static memory allocation memory is automatic allocate (at compile time or declaration time) and automatically de-locate (when Program is terminated). So far the programs given in this tutorial are examples of static memory allocation.

Dynamic memory allocation in C++

allocate memory on run-time is called dynamic memory allocation. In dynamic memory allocation, we can change the memory size at run-time 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 a  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.

Note: In dynamic memory allocation we do not declare variables at run time. Only change the memory of the previously declared variable.

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 holds 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; // allocate 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 syntax 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 Program

here is the program where we allocate the int data type memory,

using namespace std;

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

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

    cout<<*p; //print

    delete p; // memory release

return 0;




The program allocates a dynamical memory of int type from the new keyword whose address is stored in a pointer variable p* and  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 bytes because we have allocated the memory of int data type whose memory size is 2 bytes.

Free store or Heap

There is two types of memory stack and Heap, stack memory used to the execution of the program and all local variables stored in the stack memory location where Heap is an unused memory space larger then stack-memory given to the program. however both are part of RAM. For dynamic memory allocation we use heap memory location.

all primitive data-type such as int, char, float, double, long, short stored in the stack while structure, union, array, class or their objects are example of non-primitive data type stored in the Heap memory location. so now we can say,

: stack used for static memory allocation(local-variable) while free store/heap used for dynamic memory allocation(in case of pointer).

sometimes it can happen that heap does not have sufficient memory space or blocks for dynamic memory allocation in that case it’s return a NULL pointer.

memory overflow can happen by allocating multiple time dynamic memory. So it is a good practice 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.

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 fulfil 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.

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.

: Pointer-variables are stored in the stack whereas it’s allocating dynamic memory from the Heap. 

more about dynamic memory allocation,

Previous- Pointer in C++ with examples
Next- string library functions in C++ with examples

Like it?

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version