Difficulty: Beginner
What is the difference between a shallow copy and a deep copy? When does the default copy constructor cause problems?
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.
#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.
shallow copy, deep copy, copy constructor, pointers