C++ Interview Question

What is the difference between a shallow copy and a deep copy?

Updated 2026-07-10 · Beginner friendly
Quick answer

A shallow copy copies the values of an object including any pointers, so the copy and the original end up pointing to the same memory. A deep copy also duplicates whatever the pointers point to, so the copy has its own separate memory. Shallow copies can cause bugs like double frees, which is why classes that own memory need a deep copy.

The problem with shallow copies

If two objects share the same pointer after a shallow copy, freeing one frees the memory for both, and the second will point at freed memory. This causes crashes and double free errors.

// Deep copy: allocate new memory and copy the contents
MyClass(const MyClass& other) {
    data = new int(*other.data);   // separate memory
}

This is why the rule of three exists. If a class needs a custom destructor, copy constructor, or copy assignment, it usually needs all three to manage its memory correctly.

In the interview

Mention the rule of three, and the rule of five in modern C++ that adds move operations. Connecting shallow versus deep copy to these rules shows you understand why they matter, not just the definition.

Want the full C++ guide?

Read every C++ concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the C++ guide All C++ questions