Memory Management: new and delete in C++

Memory Management: new and delete in C++

Arrays can be used to store multiple homogenous data but there are serious drawbacks of using arrays.
You should allocate the memory of an array when you declare it but most of the time, the exact memory needed cannot be determined until runtime.
The best thing to do in this situation is to declare an array with maximum possible memory required (declare array with maximum possible size expected).
The downside to this is unused memory is wasted and cannot be used by any other programs.
To avoid wastage of memory, you can dynamically allocate memory required during runtime using new and delete operator in C++.

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.
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. int main()
  5. {
  6. int num;
  7. cout << "Enter total number of students: ";
  8. cin >> num;
  9. float* ptr;
  10. // memory allocation of num number of floats
  11. ptr = new float[num];
  12. cout << "Enter GPA of students." << endl;
  13. for (int i = 0; i < num; ++i)
  14. {
  15. cout << "Student" << i + 1 << ": ";
  16. cin >> *(ptr + i);
  17. }
  18. cout << "\nDisplaying GPA of students." << endl;
  19. for (int i = 0; i < num; ++i) {
  20. cout << "Student" << i + 1 << " :" << *(ptr + i) << endl;
  21. }
  22. // ptr memory is released
  23. delete [] ptr;
  24. return 0;
  25. }
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 the memory required to store num (entered by user) number of floating-point data is declared dynamically.

The new Operator

ptr = new float[num];
This expression in the above program returns a pointer to a section of memory just large enough to hold the num number of floating-point data.

The delete Operator

Once the memory is allocated using new operator, it should released back to the operating system.
If the program uses a large amount of memory using new, system may crash because there will be no memory available for the operating system.
The following expression returns memory back to the operating system.
delete [] ptr;
The brackets [] indicates the array has been 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++.
  1. #include <iostream>
  2. using namespace std;
  3. class Test
  4. {
  5. private:
  6. int num;
  7. float *ptr;
  8. public:
  9. Test()
  10. {
  11. cout << "Enter total number of students: ";
  12. cin >> num;
  13. ptr = new float[num];
  14. cout << "Enter GPA of students." << endl;
  15. for (int i = 0; i < num; ++i)
  16. {
  17. cout << "Student" << i + 1 << ": ";
  18. cin >> *(ptr + i);
  19. }
  20. }
  21. ~Test() {
  22. delete[] ptr;
  23. }
  24. void Display() {
  25. cout << "\nDisplaying GPA of students." << endl;
  26. for (int i = 0; i < num; ++i) {
  27. cout << "Student" << i+1 << " :" << *(ptr + i) << endl;
  28. }
  29. }
  30. };
  31. int main() {
  32. Test s;
  33. s.Display();
  34. return 0;
  35. }
The output of this program is same as the above program.
When the object s is created, the constructor is called which allocates the memory for num floating-point data.
When the object is destroyed, i.e, when the object goes out of scope, destructor is automatically called.
    ~Test() {
        delete[] ptr;
    }
This destructor executes delete[] ptr; and returns memory back to the operating system.

    Comments

    Popular posts from this blog

    C++ Templates ?

    C++ Program to Print Number Entered by User

    Educational Website