Arrays may be used to store multiple homogenous data but there are serious drawbacks of using arrays. Programmer should allocate the memory of an array when they declare it but most of time, the exact memory needed cannot be determined until runtime. The best thing to do in this situation is to declare the array with maximum possible memory required (declare array with maximum possible size expected) but this wastes memory.
To avoid wastage of memory, you can dynamically allocate the memory required during runtime using new and delete operator.
Example 1: C++ Memory Management
C++ Program to store GPA of n number of students and display it where n is the number of students entered by user.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int n;
cout << "Enter total number of students: ";
cin >> n;
float* ptr;
ptr = new float[n]; // memory allocation for n number of floats
cout << "Enter GPA of students." <<endl;
for (int i = 0; i < n; ++i) {
cout << "Student" << i+1 << ": ";
cin >> *(ptr + i);
}
cout << "\nDisplaying GPA of students." << endl;
for (int i = 0; i < n; ++i) {
cout << "Student" << i+1 << " :" << *(ptr + i) << endl;
}
delete [] ptr; // ptr memory is released
return 0;
}
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
In this program, only memory required to store n(entered by user) number of floating-point data is declared dynamically.
The new Operator
ptr = new float[n];
This expression in the above program returns a pointer to a section of memory just large enough to hold the n number of floating-point data.
The delete Operator
Once the memory is allocated using new operator, it should released to the operating system. If the program uses large amount of memory using new, system may crash because there will be no memory available for operating system. The following expression returns memory to the operating system.
delete [] ptr;
The brackets [] indicates that, array is deleted. If you need to delete a single object then, you don't need to use brackets.
delete ptr;
Example 2: C++ Memory Management
Object-oriented approach to handle above program in C++.
#include <iostream>
using namespace std;
class Test {
private:
int n;
float *ptr;
public:
Test() {
cout << "Enter total number of students: ";
cin >> n;
ptr = new float[n];
cout << "Enter GPA of students." <<endl;
for (int i = 0; i < n; ++i) {
cout << "Student" << i+1 << ": ";
cin >> *(ptr + i);
}
}
~Test() {
delete[] ptr;
}
void Display() {
cout << "\nDisplaying GPA of students." << endl;
for (int i = 0; i < n; ++i) {
cout << "Student" << i+1 << " :" << *(ptr + i) << endl;
}
}
};
int main() {
Test s;
s.Display();
return 0;
}
The output of this program is same as above program. When the object s is created, the constructor is called which allocates the memory for nfloating-point data.
When the object is destroyed, that is, object goes out of scope then, destructor is automatically called.
~Test() {
delete[] ptr;
}
This destructor executes delete[] ptr; and returns memory to the operating system.