C++ delete Keyword

The delete keyword in C++ has two distinct purposes. First, it is used to deallocate memory that was previously allocated using the new keyword. Second, starting from C++11, it is used to explicitly delete special member functions, preventing them from being called or instantiated. This tutorial covers both uses of the delete keyword in detail.


Syntax for Deallocating Memory

</>
Copy
delete pointer_variable;        // Deallocate memory for a single object
delete[] pointer_variable;     // Deallocate memory for an array of objects
delete
Deallocates the memory pointed to by the pointer and calls the destructor if the object has one.
pointer_variable
The pointer to the memory to be deallocated. It must point to memory allocated by new.

Syntax for Deleting Member Functions

</>
Copy
class ClassName {
public:
    ClassName(const ClassName&) = delete;           // Delete copy constructor
    ClassName& operator=(const ClassName&) = delete; // Delete copy assignment operator
};
= delete
Specifies that the compiler should delete the function, making it unusable.
ClassName
The class in which the member function is explicitly deleted.

Examples

Example 1: Deallocating Memory with delete

This example demonstrates how to allocate and deallocate memory using the new and delete keywords.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    // Allocate memory for a single integer
    int* ptr = new int(42);

    cout << "Value: " << *ptr << endl;

    // Deallocate memory
    delete ptr;

    return 0;
}

Output:

Value: 42

Explanation:

  1. Memory is dynamically allocated for an integer and initialized to 42 using new.
  2. The value is accessed and printed using the pointer.
  3. The delete keyword deallocates the memory to prevent memory leaks.

Example 2: Deleting Copy Constructor

This example demonstrates how to use the delete keyword to prevent copying of a class object.

</>
Copy
#include <iostream>
using namespace std;

class NoCopy {
public:
    NoCopy() = default;

    // Delete copy constructor
    NoCopy(const NoCopy&) = delete;

    // Delete copy assignment operator
    NoCopy& operator=(const NoCopy&) = delete;

    void display() const {
        cout << "NoCopy object" << endl;
    }
};

int main() {
    NoCopy obj1;
    obj1.display();

    // Uncommenting the following lines will cause compilation errors
    // NoCopy obj2 = obj1; // Copy constructor is deleted
    // NoCopy obj3;
    // obj3 = obj1; // Copy assignment operator is deleted

    return 0;
}

Output:

NoCopy object

Explanation:

  1. The class NoCopy explicitly deletes the copy constructor and copy assignment operator.
  2. Creating and displaying a NoCopy object works as expected.
  3. Any attempt to copy or assign a NoCopy object results in a compile-time error.

Key Points about delete Keyword

  1. The delete keyword deallocates memory that was dynamically allocated using new.
  2. For arrays allocated with new[], use delete[] to deallocate memory properly.
  3. Starting from C++11, the delete keyword can explicitly delete special member functions like constructors, assignment operators, and destructors.
  4. Using = delete prevents accidental copying or assignment of objects, ensuring better class design and avoiding unintended behavior.
  5. Always ensure that dynamically allocated memory is properly deallocated to avoid memory leaks.