Dynamic Memory Allocation (DMA) in C++
C++ allows us to allocate the memory dynamically in run time. This is known as dynamic memory
allocation.
In other programming languages such as Java and Python, the compiler automatically manages
the memories allocated to variables. But this is not the case in C++.
In C++, we need to deallocate the dynamically allocated memory manually after we have no use
for the variable.
We can allocate and then deallocate memory dynamically using the new and delete operators
respectively.
For storing the data, memory allocation can be done in two ways -
o Static allocation or compile-time allocation - Static memory allocation means providing space for the
variable. The size and data type of the variable is known, and it remains constant throughout the
program.
o Dynamic allocation or run-time allocation - The allocation in which memory is allocated dynamically.
In this type of allocation, the exact size of the variable is not known in advance. Pointers play a major
role in dynamic memory allocation.
Dynamically we can allocate storage while the program is in a running state, but variables cannot be created "on the
fly". Thus, there are two criteria for dynamic memory allocation -
o A dynamic space in the memory is needed.
o Storing the address to access the variable from the memory
Dynamic memory allocation using the new operator
o To allocate the space dynamically, the operator new is used. It means creating a request for memory
allocation on the free store. If memory is available, memory is initialized, and the address of that space is
returned to a pointer variable.
Syntax
Pointer_variable = new data_type;
The pointer_varible is of pointer data_type. The data type can be int, float, string, char, etc.
Example
int *m = NULL // Initially we have a NULL pointer
m = new int // memory is requested to the variable
Naseem Mirza
Assistant Professor
BCA Department Page 1
,It can be directly declared by putting the following statement in a line -
int *m = new int
Initialize memory
We can also initialize memory using new operator.
For example
int *m = new int(20);
Float *d = new float(21.01);
Allocate a block of memory
We can also use a new operator to allocate a block(array) of a particular data type.
For example
int *arr = new int[10]
Here we have dynamically allocated memory for ten integers which also returns a pointer to the first element of the
array. Hence, arr[0] is the first element and so on.
Note
o The difference between creating a normal array and allocating a block using new normal arrays is
deallocated by the compiler. Whereas the block is created dynamically until the programmer deletes it or
if the program terminates.
#include <iostream>
#include <memory>
using namespace std;
int main()
{
// pointer to store the address returned by the new
int* ptr;
// allocating memory for integer
ptr = new int;
// assigning value using dereference operator
*ptr = 10;
// printing value and address
cout << "Address: " << ptr << endl;
cout << "Value: " << *ptr;
return 0;
}
Address: 0x162bc20
Value: 10
Naseem Mirza
Assistant Professor
BCA Department Page 2
,delete Expression
Once we no longer need to use a variable that we have declared dynamically, we can deallocate
the memory occupied by the variable.
For this, we can use the delete expression. It returns the memory back to the operating system.
This is known as memory deallocation.
The syntax for delete expression is:
delete pointer_variable;
#include <iostream>
using namespace std;
int main() {
// dynamically allocate memory
int* point_int = new int{45};
float* point_float = new float{45.45f};
cout << *point_int << endl;
cout << *point_float << endl;
// deallocate the memory
// set pointers to nullptr
delete point_int;
delete point_float;
return 0;
}
45
45.45
Naseem Mirza
Assistant Professor
BCA Department Page 3
, // C++ program to store GPA of n number of students and display it
// where n is the number of students entered by the user
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter total number of students: ";
cin >> num;
float* ptr;
// memory allocation of num number of floats
ptr = new float[num];
cout << "Enter GPA of students." << endl;
for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": ";
cin >> *(ptr + i);
}
cout << "\nDisplaying GPA of students." << endl;
for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": " << *(ptr + i) << endl;
}
// ptr memory is released
delete[] ptr;
ptr = nullptr;
return 0;
}
Run Code
Output
Enter total number of students: 4
Enter GPA of students.
Student1: 3.6
Student2: 3.1
Student3: 3.9
Student4: 2.9
Displaying GPA of students.
Student1: 3.6
Student2: 3.1
Student3: 3.9
Student4: 2.9
Naseem Mirza
Assistant Professor
BCA Department Page 4
C++ allows us to allocate the memory dynamically in run time. This is known as dynamic memory
allocation.
In other programming languages such as Java and Python, the compiler automatically manages
the memories allocated to variables. But this is not the case in C++.
In C++, we need to deallocate the dynamically allocated memory manually after we have no use
for the variable.
We can allocate and then deallocate memory dynamically using the new and delete operators
respectively.
For storing the data, memory allocation can be done in two ways -
o Static allocation or compile-time allocation - Static memory allocation means providing space for the
variable. The size and data type of the variable is known, and it remains constant throughout the
program.
o Dynamic allocation or run-time allocation - The allocation in which memory is allocated dynamically.
In this type of allocation, the exact size of the variable is not known in advance. Pointers play a major
role in dynamic memory allocation.
Dynamically we can allocate storage while the program is in a running state, but variables cannot be created "on the
fly". Thus, there are two criteria for dynamic memory allocation -
o A dynamic space in the memory is needed.
o Storing the address to access the variable from the memory
Dynamic memory allocation using the new operator
o To allocate the space dynamically, the operator new is used. It means creating a request for memory
allocation on the free store. If memory is available, memory is initialized, and the address of that space is
returned to a pointer variable.
Syntax
Pointer_variable = new data_type;
The pointer_varible is of pointer data_type. The data type can be int, float, string, char, etc.
Example
int *m = NULL // Initially we have a NULL pointer
m = new int // memory is requested to the variable
Naseem Mirza
Assistant Professor
BCA Department Page 1
,It can be directly declared by putting the following statement in a line -
int *m = new int
Initialize memory
We can also initialize memory using new operator.
For example
int *m = new int(20);
Float *d = new float(21.01);
Allocate a block of memory
We can also use a new operator to allocate a block(array) of a particular data type.
For example
int *arr = new int[10]
Here we have dynamically allocated memory for ten integers which also returns a pointer to the first element of the
array. Hence, arr[0] is the first element and so on.
Note
o The difference between creating a normal array and allocating a block using new normal arrays is
deallocated by the compiler. Whereas the block is created dynamically until the programmer deletes it or
if the program terminates.
#include <iostream>
#include <memory>
using namespace std;
int main()
{
// pointer to store the address returned by the new
int* ptr;
// allocating memory for integer
ptr = new int;
// assigning value using dereference operator
*ptr = 10;
// printing value and address
cout << "Address: " << ptr << endl;
cout << "Value: " << *ptr;
return 0;
}
Address: 0x162bc20
Value: 10
Naseem Mirza
Assistant Professor
BCA Department Page 2
,delete Expression
Once we no longer need to use a variable that we have declared dynamically, we can deallocate
the memory occupied by the variable.
For this, we can use the delete expression. It returns the memory back to the operating system.
This is known as memory deallocation.
The syntax for delete expression is:
delete pointer_variable;
#include <iostream>
using namespace std;
int main() {
// dynamically allocate memory
int* point_int = new int{45};
float* point_float = new float{45.45f};
cout << *point_int << endl;
cout << *point_float << endl;
// deallocate the memory
// set pointers to nullptr
delete point_int;
delete point_float;
return 0;
}
45
45.45
Naseem Mirza
Assistant Professor
BCA Department Page 3
, // C++ program to store GPA of n number of students and display it
// where n is the number of students entered by the user
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter total number of students: ";
cin >> num;
float* ptr;
// memory allocation of num number of floats
ptr = new float[num];
cout << "Enter GPA of students." << endl;
for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": ";
cin >> *(ptr + i);
}
cout << "\nDisplaying GPA of students." << endl;
for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": " << *(ptr + i) << endl;
}
// ptr memory is released
delete[] ptr;
ptr = nullptr;
return 0;
}
Run Code
Output
Enter total number of students: 4
Enter GPA of students.
Student1: 3.6
Student2: 3.1
Student3: 3.9
Student4: 2.9
Displaying GPA of students.
Student1: 3.6
Student2: 3.1
Student3: 3.9
Student4: 2.9
Naseem Mirza
Assistant Professor
BCA Department Page 4