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.
delete
frees the memory that was allocated using new.delete
:delete
for deallocating memory allocated for a single object.delete[]
for deallocating memory allocated for an array of objects.delete
is called, it automatically calls the destructor of the object (if the object has a destructor) before deallocating the memory.delete
on memory that wasn't allocated with new
, or using delete
on the same pointer more than once, leads to undefined behavior.#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;
}
MyClass* ptr = new MyClass();
dynamically allocates memory for a MyClass
object and calls the constructor.delete ptr;
deallocates the memory pointed to by ptr
and calls the destructor of MyClass
.delete
is called, allowing for proper resource cleanup.delete
with an ArrayWhen 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;
}
MyClass* ptr = new MyClass[3];
dynamically allocates memory for an array of three MyClass
objects.delete[] ptr;
deallocates the memory for the entire array and calls the destructor for each element in the array.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;
}
int(10);
allocation is not followed by delete ptr;
, and ptr
is reassigned, the memory originally allocated is not deallocated, resulting in a memory leak.delete
: Always ensure that delete
is called for every new
to prevent memory leaks.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;
}
delete
(if uncommented) would attempt to deallocate memory that has already been freed, which is undefined behavior and can cause a crash or other issues.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;
}
delete
Keyword: Used to deallocate memory that was allocated using new. It ensures that dynamically allocated memory is properly freed to avoid memory leaks.delete[]
for arrays to deallocate memory correctly.delete
is called, the destructor of the object is automatically invoked to perform any necessary cleanup.delete
is crucial for preventing memory leaks and ensuring efficient memory management in C++ programs.delete
on uninitialized pointers or pointers that have already been deleted to prevent undefined behavior.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.