C++ new & delete
In this page:
Dynamic Memory Allocation
The new operator requests a block of memory from the heap at runtime and returns a pointer to it, letting you create objects whose size or lifetime isn't known when the program starts. Every successful new must eventually be paired with a matching delete, or that memory stays reserved for the rest of the program's run -- a memory leak.
Example: Dynamic Memory Allocation
#include <iostream>
int main() {
int *ptr = new int(5);
std::cout << *ptr << std::endl;
delete ptr;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Arrays
delete[] releases memory that was allocated with new[], and must be used instead of a plain delete for arrays -- delete alone only knows how to destroy a single object, not walk through and destruct every element of an array first. Mismatching new[] with delete (or vice versa) is undefined behavior.
Example: Dynamic Arrays
#include <iostream>
int main() {
int *arr = new int[3]{1, 2, 3};
std::cout << arr[1] << std::endl;
delete[] arr;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Object Creation
new ClassName(args) allocates space for an object and immediately calls its constructor, so the object is fully initialized as soon as the pointer is returned. delete on that pointer runs the destructor first, then frees the underlying memory -- this is how you get RAII-style cleanup even for heap objects.
Example: Dynamic Object Creation
#include <iostream>
class Point {
public:
int x;
Point(int val) : x(val) { std::cout << "Constructed" << std::endl; }
~Point() { std::cout << "Destructed" << std::endl; }
};
int main() {
Point *p = new Point(5);
std::cout << p->x << std::endl;
delete p;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Guarding against Out of Memory
Passing std::nothrow to new (e.g. new(std::nothrow) int[100]) tells it to return a null pointer on allocation failure instead of throwing std::bad_alloc, which is useful in code that can't safely handle C++ exceptions. You must then check the returned pointer before using it.
Example: Guarding against Out of Memory
#include <iostream>
#include <new>
int main() {
int *arr = new(std::nothrow) int[100];
if (arr == nullptr) {
std::cout << "Allocation failed" << std::endl;
} else {
std::cout << "Allocation succeeded" << std::endl;
}
delete[] arr;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Rules and Memory Leak Protection
Setting a pointer to nullptr immediately after delete prevents a dangling pointer -- one that still holds an address but no longer points to valid memory. Using or deleting a dangling pointer again is undefined behavior, so this simple habit avoids a whole class of hard-to-debug crashes.
Example: Rules and Memory Leak Protection
#include <iostream>
int main() {
int *ptr = new int(5);
delete ptr;
ptr = nullptr;
std::cout << (ptr == nullptr) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: