Difference Between Delete And Delete In C++ - C++ Programming Tutorial
C++ Course / C++ vs Other Languages / Difference Between Delete And Delete In C++

Difference Between Delete And Delete In C++

BLUF: Mastering Difference Between Delete And Delete In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Difference Between Delete And Delete In C++

C++ is renowned for its efficiency. Learn how Difference Between Delete And Delete In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, we will explore the variances between Delete and Delete in C++. Prior to delving into their distinctions, it is essential to understand Delete and Delete in C++ along with illustrative examples.

Overview of Delete:

Delete can be used to deallocate memory that was dynamically created for a single object using new function . It will ensure that our object's destructor, if it exists, gets called before memory is released. The syntax is delete pointer, where pointer is the address of the allocated memory. Invalid behavior occurs when the delete function is used on memory that was not created by new or on a null pointer. Delete must be used carefully to prevent memory leaks and ensure that system resources are released appropriately when an object is no longer needed.

  • It is used to free up memory assigned to a specific object.
  • Syntax: delete pointer;
  • It de-allocates the memory during the invocation of the destructor, if any, of the single object that the pointer points on.
  • Example:

Example

int* p = new int(10);  // dynamically allocate memory for a single integer
delete p;              // deallocates memory for the single integer

Implementation Code using Delete for Single Object:

Example

#include <iostream>
using namespace std;

class MyClass {
public:
    MyClass() {
        cout << "Constructor called!" << endl;
    }
    ~MyClass() {
        cout << "Destructor called!" << endl;
    }
};

int main() {
    // Dynamically allocating memory for a single object
    MyClass* obj = new MyClass;

    // Releasing memory for the single object
    delete obj;  // Calls the destructor and deallocates memory

    return 0;
}

Output:

Output

Constructor called!
Destructor called!

Explanation:

In this instance, the destructor is invoked once the delete operator releases the memory allocated dynamically to a singular instance of the MyClass class through the new keyword.

Overview of Delete function:

Memory is used to store an array of objects, when new function can be freed up by using delete. The array's destructor calls each of its elements to ensure that resources are completely freed before memory is released. Syntax for the dynamically allocated array is delete pointer, where pointer is the first element. It is always be used to prevent memory issues, as using delete on arrays or delete on individual objects results in undefined behavior. When delete is used appropriately, memory is restored and every element of the array gets deleted as correctly.

  • It is utilized to release memory that was set allocated for a variety of objects.
  • Syntax: delete pointer;
  • This de-allocates the memory after invoking each object's destructor for that particular array.
  • Example:

Example

int* arr = new int[5];  // dynamically allocate memory for an array of 5 integers
delete[] arr;  // deallocates memory for the entire array

Implementation code using Delete for array of objects:

Example

#include <iostream>
using namespace std;

class MyClass {
public:
    MyClass() {
        cout << "Constructor called!" << endl;
    }
    ~MyClass() {
        cout << "Destructor called!" << endl;
    }
};

int main() {
    // Dynamically allocating memory for an array of objects
    MyClass* arr = new MyClass[3];  // Allocates memory for an array of 3 objects

    // Releasing memory for the array of objects
    delete[] arr;  // Calls the destructor for each object and deallocates memory

    return 0;
}

Output:

Output

Constructor called!
Constructor called!
Constructor called!
Destructor called!
Destructor called!
Destructor called!

Explanation:

By calling the destructor of every object within the array, the delete operation releases memory following the dynamic allocation of three objects using new.

Key differences between Delete and Delete function:

  • Use delete function for single objects.
  • Use delete function for object arrays.
  • Delete function makes a single object destructor call.
  • For every entry into the array, the destructor is called via delete.
  • Undefined behavior can result from using delete on an array or delete on a single object.
  • Therefore, the allocation type must be matched with the appropriate form of delete.
  • Conclusion:

In summary, understanding the significance of delete and delete is crucial in managing dynamic memory effectively. When dynamically allocating objects, releasing this memory is a critical consideration. The delete operator is utilized to free up memory allocated for a single object, while delete is employed for arrays of objects. Misusing these operators can result in unpredictable outcomes, potentially leading to system crashes or memory leaks due to incorrect usage (such as using delete with an array or delete with a single object). Adhering to the practice of pairing new with delete and new with delete for memory allocation ensures proper resource handling, eliminates memory leaks, and facilitates the correct invocation of destructors.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience