# Shallow copy vs deep copy

> What is the difference between a shallow copy and a deep copy? When does the default copy constructor cause problems?

Canonical: https://www.internhack.in/learn/interview/cpp-interview/cpp-shallow-vs-deep-copy
Last updated: 2026-11-28

Difficulty: Beginner

## Question

What is the difference between a shallow copy and a deep copy? When does the default copy constructor cause problems?

## Answer

Copying sounds like the simplest thing a program does, yet this topic causes real crashes. Let's see exactly why, using an analogy first.

Imagine you have a document on Google Docs. A shallow copy is like sharing the link with a friend: you now have two names for the same document, and if either of you edits it, the other sees the change. A deep copy is like downloading the document and giving your friend their own file: from that moment the two are independent. In C++ terms, a shallow copy copies member values bit by bit, so pointer members end up pointing to the same memory. A deep copy allocates new memory for the pointed-to data and copies the contents.

The compiler-generated copy constructor and copy assignment do a memberwise copy, which is a shallow copy of any raw pointer. For classes without pointers (numbers, strings, vectors) that is perfectly fine, because members like std::vector have their own correct copy constructors, so the memberwise copy is effectively deep. The problem appears when a class owns a raw pointer allocated with new. After a default copy, two objects hold the same address. Change through one, and the other sees it, which is often unintended. Worse, when the first object is destroyed it deletes the memory, leaving the second with a dangling pointer, and when the second is destroyed it deletes the same address again, a double free that usually crashes the program.

The fix is to define a copy constructor and a copy assignment operator that allocate new storage and copy the data (deep copy), along with a destructor that frees it. This is the rule of three in action. Better still, avoid raw pointers by holding a std::vector or std::string, or use std::unique_ptr, which makes the class non-copyable by default and forces you to think about it.

There is also a design choice. Sometimes sharing is exactly what you want and is cheaper: std::shared_ptr gives you intentional, reference-counted shallow copies, and copy-on-write strings used to do the same. The key is that the sharing must be deliberate and managed by a type that knows how to release the resource exactly once.

When answering in an interview, define both terms, describe the double free scenario, and explain the fix. Then add the modern twist: prefer the rule of zero so that you rarely have to write copy logic by hand. Also mention that when copying objects with self-assignment such as a = a, a naive deep-copy assignment that deletes first and copies afterward would destroy its own data, so guard with a self-check or use copy-and-swap.

## Code examples

### Shallow sharing vs deep copy

```
#include <iostream>

// Deep-copying wrapper around a heap int.
class Deep {
    int* p_;
public:
    explicit Deep(int v) : p_(new int(v)) {}
    Deep(const Deep& o) : p_(new int(*o.p_)) {}          // allocate new storage
    Deep& operator=(const Deep& o) {
        if (this != &o) *p_ = *o.p_;                       // copy value, keep own storage
        return *this;
    }
    ~Deep() { delete p_; }
    int& value() { return *p_; }
};

int main() {
    // Shallow: two raw pointers to the same object
    int* a = new int(10);
    int* b = a;                 // copies the address only
    *b = 99;
    std::cout << *a << "\n";    // a sees the change
    delete a;                   // b is now dangling; do not use it

    // Deep: independent copies
    Deep d1(10);
    Deep d2 = d1;
    d2.value() = 99;
    std::cout << d1.value() << " " << d2.value() << "\n";
}
```

Copying a raw pointer shares the pointee, so writes through b affect a. The Deep class owns its own int, so d1 is untouched by changes to d2.

## Key points

- Shallow copy duplicates pointer values; deep copy duplicates the pointed-to data.
- The default copy is memberwise, which is shallow for raw pointers.
- Shared raw pointers lead to double delete and dangling pointers.
- Fix with a custom copy constructor, assignment, and destructor, or better, use vector, string or smart pointers.
- Intentional sharing should use shared_ptr, not accidental raw pointer copies.

## Common follow-up questions

- Does copying a std::vector<int\*> copy the pointed-to ints?
- How does a move differ from a shallow copy?
- What happens on self-assignment in a naive deep-copy operator=?

## Interview tips

- Use the shared-link versus downloaded-file analogy, then quickly move to the double free failure.
- State the fix as rule of three, then mention you would prefer vector or unique_ptr.

## Concepts covered

shallow copy, deep copy, copy constructor, pointers

Previous: [Constructors in C++: default, copy and move](https://www.internhack.in/learn/interview/cpp-interview/cpp-constructors-copy-move) · Next: [Rule of three, rule of five and rule of zero](https://www.internhack.in/learn/interview/cpp-interview/cpp-rule-of-three-five-zero) · [All of C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)

## More C++ & STL questions

- [Lvalues, rvalues and move semantics](https://www.internhack.in/learn/interview/cpp-interview/cpp-lvalue-rvalue-move-semantics)
- [Copy elision, RVO and NRVO](https://www.internhack.in/learn/interview/cpp-interview/cpp-copy-elision-rvo)
- [std::move vs std::forward and perfect forwarding](https://www.internhack.in/learn/interview/cpp-interview/cpp-std-move-vs-forward)
- [Virtual functions, vtable and vptr](https://www.internhack.in/learn/interview/cpp-interview/cpp-virtual-functions-vtable)
- [Pure virtual functions and abstract classes](https://www.internhack.in/learn/interview/cpp-interview/cpp-pure-virtual-abstract-classes)
- [Why do base classes need a virtual destructor?](https://www.internhack.in/learn/interview/cpp-interview/cpp-virtual-destructor)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)
