How Do new and delete Work in C++?
Understand how C++ new and delete allocate and free heap memory, pair new[]/delete[], avoid leaks and dangling pointers, with examples and interview tips.
Expected Interview Answer
The new operator allocates memory on the heap and constructs an object in it, returning a typed pointer, while delete calls the object's destructor and returns that memory to the free store.
new does two things: it requests raw storage (via operator new) and then runs the constructor; delete runs the destructor and then releases the storage (via operator delete). For arrays you must pair new[] with delete[] so every element's destructor runs. Every new must have exactly one matching delete — omitting it leaks memory, and deleting twice or mismatching new[]/delete causes undefined behaviour.
- Allocates objects with lifetime independent of scope
- Runs constructors and destructors automatically
- Enables dynamic data structures like linked lists and trees
- Returns a correctly typed pointer, no cast needed
- Throws std::bad_alloc on failure rather than returning null
AI Mentor Explanation
Think of new as the groundsman rolling out a fresh practice pitch on demand and marking the creases so it is ready to play, while delete is the crew tearing that pitch up and reclaiming the strip when the session ends. Forget to reclaim it and the outfield slowly fills with abandoned pitches nobody can use again for the next match.
Step-by-Step Explanation
Step 1
Request storage
new calls operator new to obtain raw memory of the right size from the heap.
Step 2
Construct the object
The constructor runs in that memory, initializing the object and returning a typed pointer.
Step 3
Use the pointer
Access members through the pointer while the object is alive.
Step 4
Destroy the object
delete calls the destructor to release any resources the object held.
Step 5
Release storage
operator delete returns the raw memory to the free store for reuse.
What Interviewer Expects
- Knows new both allocates and constructs, delete destructs and frees
- Pairs new[] with delete[] correctly
- Understands every new needs exactly one delete
- Can name leaks, double frees and dangling pointers as risks
- Mentions smart pointers as the modern alternative
Common Mistakes
- Pairing new[] with delete instead of delete[]
- Forgetting delete, causing a memory leak
- Deleting the same pointer twice (double free)
- Using a pointer after deleting it (dangling pointer)
- Assuming new returns null instead of throwing std::bad_alloc
Best Answer (HR Friendly)
“In C++, new sets aside memory for a new object while the program runs and gets it ready to use, and delete gives that memory back when you're done. If you forget to give it back, the program slowly wastes memory, which is why modern C++ prefers smart pointers to handle it automatically.”
Code Example
#include <iostream>
struct Point {
int x, y;
Point(int a, int b) : x(a), y(b) {}
~Point() { std::cout << "destroyed (" << x << "," << y << ")\n"; }
};
int main() {
// Single object: new allocates + constructs.
Point* p = new Point(3, 4);
std::cout << p->x << "," << p->y << "\n";
delete p; // destructor runs, memory freed
// Array: pair new[] with delete[].
int* nums = new int[5]{1, 2, 3, 4, 5};
std::cout << nums[2] << "\n";
delete[] nums; // delete[], NOT delete
return 0;
}Follow-up Questions
- What is the difference between delete and delete[]?
- What happens if you forget to call delete?
- What is a dangling pointer and how do you avoid one?
- How does new report allocation failure?
- Why do smart pointers reduce the need for manual delete?
MCQ Practice
1. What two actions does the new operator perform?
new first obtains raw storage via operator new, then runs the constructor in that storage.
2. Which correctly frees memory from int* a = new int[10];?
Array allocations made with new[] must be released with delete[] so all elements are handled.
3. On failure to allocate, what does new do by default?
By default new throws std::bad_alloc; the nothrow variant is needed to get a null pointer instead.
Flash Cards
What does new do? — Allocates heap storage and constructs the object, returning a typed pointer.
What does delete do? — Runs the destructor, then returns the storage to the free store.
Array allocation pairing? — new[] must be matched with delete[], never plain delete.
Result of forgetting delete? — A memory leak — the storage is never reclaimed.
How does new signal failure? — It throws std::bad_alloc by default.