C++ Dynamic Memory

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

In this C++ tutorial, let us know about C++ Dynamic Memory, Operators for allocation and Sample program in C++.

Introduction to Dynamic Memory in C++

Dynamic Memory in the C++ program can be split into two sections,

  1. Stack
  2. Heap

In Stack, all variables are declared inside the function will take up a memory from the stack.

A Heap is the unused memory of the program and can be applied to allocate the memory dynamically when the program runs.

Using the new operator, you can allocate memory at run time for the given variable within the heap, that operator returns the address of the allocated space.

If you need not require the dynamically allocated memory, then you can use a delete operator which de-allocates the memory previously allocated by the new operator.

Syntax of Operator new & delete

The syntax for new and delete operator to allocate/de-allocate the dynamic memory for any data type as follows,


new data-type;


delete acc_value; 

Let us put the above concepts and form the following example program to show how ‘new’ and ‘delete’ works, 

C++ Program to understand Dynamic Memory

#include <iostream>
using namespace std;
int main () {
   double* acc_value = NULL; // Pointer initialized with null
   acc_value = new double; // Request memory for the variable 
   *acc_value = 29494.99; // Store value at allocated address
   cout << "Value of acc_value : " << *acc_value << endl;
   delete acc_value; // free up the memory.
   return 0;


Value of acc_value : 29495

Leave a Comment

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