delete


Concept: delete

The delete keyword in C++ is used to deallocate memory that was previously allocated using the new keyword. Proper use of delete is essential to manage memory in C++ programs, especially to avoid memory leaks, which occur when dynamically allocated memory is not properly deallocated.

Key Characteristics of delete

Example 1: Using delete with a Single Object

#include <iostream>

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

int main() {
    MyClass* ptr = new MyClass();  // Dynamically allocate memory for a MyClass object
    delete ptr;  // Deallocate the memory and call the destructor

    return 0;
}

Explanation:

Example 2: Using delete with an Array

When allocating memory for an array of objects using new[], you must use delete[] to deallocate the memory.

#include <iostream>

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

int main() {
    MyClass* ptr = new MyClass[3];  // Allocate memory for an array of 3 MyClass objects

    delete[] ptr;  // Deallocate the memory for the entire array and call destructors

    return 0;
}

Explanation:

Example 3: Avoiding Memory Leaks with delete

A memory leak occurs when dynamically allocated memory is not deallocated before the pointer to it is reassigned or goes out of scope.

#include <iostream>

int main() {
    int* ptr = new int(10);  // Allocate memory for an integer

    // Forgetting to delete the pointer results in a memory leak
    // delete ptr;  // Uncommenting this line would prevent the memory leak

    ptr = new int(20);  // Memory for the first integer is lost (leak)

    delete ptr;  // Deallocate memory for the second integer

    return 0;
}

Explanation:

Example 4: Double delete and Undefined Behavior

Calling delete on the same pointer more than once results in undefined behavior, which can lead to program crashes or other unexpected issues.

#include <iostream>

int main() {
    int* ptr = new int(10);  // Allocate memory for an integer

    delete ptr;  // Deallocate the memory

    // Deleting the same pointer again leads to undefined behavior
    // delete ptr;  // Uncommenting this line could cause a crash

    return 0;
}

Explanation:

Example 5: Using `delete. in a Class with a Destructor

In classes that allocate dynamic memory, it is common practice to use d.elete .in the destructor to ensure proper cleanup.

#include <iostream>

class MyClass {
private:
    int* data;
public:
    MyClass(int size) {
        data = new int[size];  // Dynamically allocate memory for an array
        std::cout << "Memory allocated." << std::endl;
    }

    ~MyClass() {
        delete[] data;  // Deallocate the memory in the destructor
        std::cout << "Memory deallocated." << std::endl;
    }
};

int main() {
    MyClass obj(10);  // Allocate and deallocate memory automatically

    return 0;
}

Explanation:

Summary

Understanding and correctly using delete is essential for managing dynamic memory in C++. It ensures that resources are properly freed, and your programs run efficiently without memory-related issues.

Previous Page | Course Schedule | Course Content