Dynamic Memory Allocation in C++

Dynamic Memory Allocation in C++ can be defined to perform memory allocation manually by the programmer.  The dynamically allocated memory is allocated on Heap. All variables declared inside the function will take up a memory from the stack. All the local variables get memory allocated on Stack.  Static memory allocation consists of allocating memory in compile-time before the program is executed.

Dynamic Memory Allocation in C++

Stack

A stack is the special region of the computer’s memory, where temporary variables are stored. Stack follows the First In Last Out (FILO) data structure. When a function declares a variable, this variable is pushed into the stack. Once the function exits, the variable is popped from the stack.

Dynamic memory allocation in c++



Heap

A heap is a region of computer’s memory, used for dynamic memory allocation. When you use dynamic allocation, all the created variables are stored into a heap. Heap memory is not managed automatically. When you use dynamic memory allocation, a pointer that is located in stack points to the region of the allocated memory in heap.

heap memory allocation in c++

New Operator in C++

The new operator in C++ can be used for dynamic memory allocation. Operator new returns the pointer to the newly allocated space. If you want to allocate memory for one single element of a specified data type (it can be a built-in data type, structure or class).

Syntax: new data_type;

In case if you would like to allocate memory in arrays, follow the order as follows:

Syntax: new data_type[size_of_array];

Initialize memory

We can also initialize the memory using the new operator.

Syntax: pointer-variable = new data-type(value);

Example:

int *p = new int(25);
float *q = new float(75.25);

Allocate a block of memory

The new operator is also used to allocate a block(an array) of memory of type data-type.
Syntax: pointer-variable = new data-type[size];

Example:
int *p = new int[10]

Example:

int* arr;//pointer to int
int n;//number of elements
cout << "Please, enter the number of elements for input" << endl;
cin >> n; // get n
arr = new int[n];//allocate memory for array of int of size n
//get user’s input
cout<<"Enter " << n << " elements" << endl;
//get elements in loop
for (int i = 0; i != n; ++i)
  cin >> arr[i];
cout<<"You entered :" << endl;
for (int i = 0; i != n; ++i)
  cout << "arr[" << i << "] = " << arr[i] << endl;

Output:

Please, enter the number of elements for input
5
Enter five elements
1
2
3
4
0
You entered:
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 0

delete Operator in C++

Once you do not need memory allocated by operator new, you have to release it. You can do it by using operator delete:

Syntax:

delete pointer; for single object and

delete[ ] pointer; for an array of objects

Example:

#include<iostream> 
using namespace std; 
int main () 
{ 
//Pointer initialization to null 
    int* p = NULL; 
//Request memory for the variable using new operator 
    p = new(nothrow) int; 
    if (!p) 
        cout<<"allocation of memory failed\n"; 
    else
    { 
        //Store value at allocated address 
        *p = 29; 
        cout<< "Value of p: "<< *p << endl; 
    } 
  
    //Request block of memory 
    //using new operator 
    float *r = new float(75.25); 
    cout<<"Value of r: " << *r << endl; 
  //Request block of memory of size n 
    int n = 5; 
    int *q = new(nothrow) int[n]; 
    if (!q) 
        cout<<"allocation of memory failed\n"; 
    else
    { 
        for(int i = 0; i < n; i++) 
            q[i] = i+1; 
  
        cout<<"Value store in block of memory: "; 
        for(int i = 0; i < n; i++) 
            cout << q[i] << " "; 
    } 
  //freed the allocated memory 
    delete p; 
    delete r; 
 //freed the block of allocated memory 
    delete[] q;  
    return 0; 
}

Output:

Value of p: 29
Value of r: 75.25
Value store in block of memory: 1 2 3 4 5