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:
- Memory is dynamically allocated for an integer and initialized to 
42usingnew. - The value is accessed and printed using the pointer.
 - The 
deletekeyword 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:
- The class 
NoCopyexplicitly deletes the copy constructor and copy assignment operator. - Creating and displaying a 
NoCopyobject works as expected. - Any attempt to copy or assign a 
NoCopyobject results in a compile-time error. 
Key Points about delete Keyword
- The 
deletekeyword deallocates memory that was dynamically allocated usingnew. - For arrays allocated with 
new[], usedelete[]to deallocate memory properly. - Starting from C++11, the 
deletekeyword can explicitly delete special member functions like constructors, assignment operators, and destructors. - Using 
= deleteprevents accidental copying or assignment of objects, ensuring better class design and avoiding unintended behavior. - Always ensure that dynamically allocated memory is properly deallocated to avoid memory leaks.
 
