Dynamic memory is different from static memory because dynamic memory is allocated at run-time, whereas static memory is allocated at compile time.
Static memory normally stores data of fixed size. All basic data types use static memory, each having a specific size, e.g., char = 1 byte, int = 4 bytes.
Dynamic memory stores data whose size is not known at compile-time, e.g., taking input of unknown size from users.
To create dynamic memory in C++, we use the keyword new
. We need to provide new
with the basic data type of the dynamic array to be created.
int* ptr = new int;
new
allocates memory of the specified size and returns a pointer pointing at the start of the allocated memory. The code below creates a dynamic memory of 40 bytes, i.e., 10 x 4 (size of an integer):
int* ptr = new int[10];
Since the memory is of the int
type, the returned pointer is saved inside an integer pointer. We can now iterate this newly allocated dynamic memory as a normal C++ array.
for(int i=0; i<10 i++)ptr[i] = i;
To increase the size of the allocated memory, we have to allocate a new memory of the required size. We copy the previous data into the new allocation:
int* temp = new int[20];for(int i=0; i<10 i++)temp[i] = ptr[i];delete(ptr);ptr = temp;
The code increases the size of a dynamic array.
temp
.ptr
.ptr
to the new memory pointed by temp
.#include <iostream>using namespace std;void print_int_array(int* ptr, int size){for(int i=0;i<size;i++)cout << ptr[i] << " ";cout << endl;}int main() {int* ptr = new int[10];for(int i=0;i<10;i++){ptr[i]=i;}print_int_array(ptr,10);int* temp= new int[20];for(int i=0;i<10;i++){temp[i]=ptr[i];}delete(ptr);ptr=temp;print_int_array(ptr,20);delete(ptr);return 0;}
In C++, the memory management for dynamic memory is not done automatically. It’s our responsibility to delete each allocated memory once its purpose is fulfilled. The memory that is not deleted remains allocated until the process is complete and is unavailable for other processes causing poor memory management.
Note:
in C++ provides multiple data structures like STL Standard Template Library vector
andmap
based on dynamic memory allocation. Their implementations ensure that the destructor deletes all types of dynamic memory allocated.
C#, given its object-oriented approach, provides multiple classes to allocate dynamic memory ArrayList
and List
.
var arraylist = new ArrayList();var list = new List();
The classes for dynamic memory in C# provide auto-growth functionality, i.e., we do not need to reallocate manually if the size is to be increased.
using System;using System.Collections;using System.Collections.Generic;class DynMem {static public void Main(){var list = new List<int>();for (int i=0; i<10; i++)list.Add(i);foreach(int value in list)Console.Write(value + " ");}}
Line 9: We allocate a new dynamic memory using List()
.
Lines 10-11: We populate ten indices by automatic growth.
Lines 12-13: We print the values in the allocated memory.
Note: C# has an automatic process of memory management and does not require each block of dynamic memory allocated to be deleted manually. A program named Garbage Collector manages the memory deleting blocks that are not accessible by a process.