← Back to C++ Course | Chapter 14: Memory Management | Lesson 2 of 6

C++ new & delete

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

cpp
#include <iostream>

int main() {
	int *ptr = new int(5);
	std::cout << *ptr << std::endl;
	delete ptr;
	return 0;
}

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

cpp
#include <iostream>

int main() {
	int *arr = new int[3]{1, 2, 3};
	std::cout << arr[1] << std::endl;
	delete[] arr;
	return 0;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#include <iostream>

int main() {
	int *ptr = new int(5);
	delete ptr;
	ptr = nullptr;
	std::cout << (ptr == nullptr) << std::endl;
	return 0;
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.