C++ Dynamic Memory Allocation

C++ allows us to write programs that can dynamically adjust their memory requirement at runtime. If a program uses dynamic allocation of memory, it saves resources and uses only the needed amount. C++ uses pointers together with two other operators to achieve this.

Static and Dynamic Memory Allocation

In programming, a memory allocation is when you assign a memory section to a program for its use.

Static Memory Allocation

In static memory allocation, the exact memory requirement is known and allocated during the compile time. The regular variables and arrays that we use in our programs are allocated space in the memory before the program executes.

Dynamic Memory Allocation

In dynamic memory allocation, the exact memory requirement of a program is not known during the compile time. It can allocate any amount of memory as and when required during the runtime and use it. It is also possible to free the dynamically allocated memory once it is not required.

A program can use both static and dynamic memory allocation of memory.

Dynamic Memory Allocation Operators

C++ provides two operators – new and delete to work with dynamic memory allocation.

“new” operator

The new operator allocates a block of free memory for the specified type of data. Its general syntax is:

pointer = new datatype;

Once the memory allocation is successful, it returns a pointer that points to the newly allocated memory. Thus, we can store and access this memory using a pointer.

The example below allocates memory for an integer (4 bytes) and stores the allocated memory address in a pointer.

int * ptr;
/* store the pointer returned by "new" for further use */
ptr = new int;
/* access the newly allocated value through the pointer */
* ptr = 10;

“delete” operator

The delete operator removes the block of memory that was previously allocated using the new operator. Therefore, if the program does not require any memory location, it should be freed using the delete operator. This helps to avoid any memory leaks.\

The syntax of the delete operation is:

delete pointer;

C++ Dynamic Arrays

Normal C++ arrays are static. We have to specify the exact size or the number of elements during its declaration. It is very limited as we cannot increase or decrease its size once the program starts.

Dynamic arrays are flexible as they can be created during runtime with a size that can be determined dynamically. To create a dynamic array, we need to specify its size with the new operator:

arraypointer = new datatype[size];

To delete a dynamic array, we need to use a similar syntax:

delete [size] arraypointer;

The example below reads the size of the data from the user and creates a dynamic array.


using namespace std;

int main() {
  /* 1. Declare a size and a pointer to store the dynamic array */
  int size, * ptr;
  /* 2. Read the dynamic size from the user */
  cout << "Enter the number of data to store (arraysize): ";
  cin >> size;
  /* 3. Create a dynamic array with the size read from the user */
  ptr = new int[size];
  /* Now pointer “ptr” can be used like any other normal array. We can either use ptr[i] or *(ptr + i) with pointers */

  /* 4. Next read the values into the dynamic array */
  for (int i = 0; i < size; i++) {
    cout << "Enter value " << i + 1 << ": ";
    cin >> ptr[i];
  /* Display the values of the dynamic array */
  cout << "The dynamic array is: " << endl;
  for (int i = 0; i < size; i++) {
    cout << ptr[i] << " ";

Points to Remember

  • Dynamic Memory Allocation is a useful feature that C++ programs can utilize to write memory-efficient programs.
  • Static memory is allocated during the compile-time, and it cannot be changed during runtime.
  • Dynamic memory is allocated using the new operator, and the returned value must be stored in a pointer for using it.
  • The dynamically allocated memory is deleted using the delete operator.
  • Dynamic arrays are created using a size that is determined at runtime.
  • Linked Lists, Binary Trees, and other data structures use dynamic memory allocation to store data.
Back to: Learn C++ Programming > Pointers in C++

Leave a Reply

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