Shallow Copy and Deep Copy in C++

Last Updated : 17 Jan, 2026

In C++, creating a copy of an object involves duplicating its data and resources using the copy constructor or the assignment operator. Depending on how the object manages dynamic memory and other resources, copying can be performed using either Shallow Copy or Deep Copy. Choosing the correct copying approach is important to prevent memory-related errors and ensure proper resource management.

Shallow Copy

  • In shallow copy, all variable values are copied, but dynamically allocated memory addresses are shared between objects.
  • Both objects point to the same heap memory, so changes made through one object affect the other.
  • This can lead to runtime errors like dangling pointers and double deletion, so it does not create an independent copy.

Note: C++ compiler implicitly creates a copy constructor and overloads assignment operator in order to perform shallow copy at compile time.

Example 1:

C++
#include <iostream>
using namespace std;

int main() {
    int a[3] = {10, 20, 30};
    int* b = a;   // shallow copy of address

    b[0] = 99;
    cout << a[0] << endl;
    cout << b[0] << endl;
}

Output
99
99

Explanation: In this program, b = a copies only the address, so both pointers share the same memory and any change in b also changes a.

Example 2:

C++
#include <iostream>
using namespace std;

class Sample {
public:
    int *x;

    Sample(int val) {
        x = new int;
        *x = val;
    }
};

int main() {
    Sample obj1(10);     // Original object
    Sample obj2 = obj1; // Shallow copy

    *obj2.x = 50;       // Modify copied object

    cout << "obj1.x = " << *obj1.x << endl;
    cout << "obj2.x = " << *obj2.x << endl;

    return 0;
}

Output
obj1.x = 50
obj2.x = 50

Explanation:

  • "obj1" is created and memory is allocated for variable x.
  • "obj2" = "obj1" copies only the memory address of x, not the actual data.
  • Both objects now point to the same heap memory.
  • Changing obj2.x also changes obj1.x.

Deep Copy

  • In deep copy, a new object is created by copying all variable values and allocating separate memory for dynamically allocated variables.
  • Both the original and copied objects store their data in different memory locations.
  • Changes made to one object do not affect the other because a custom copy constructor is used.

Example 1:

C++
#include <iostream>
using namespace std;

int main() {
    int a[3] = {10, 20, 30};   
    int b[3];               

    for (int i = 0; i < 3; i++)
        b[i] = a[i];
        
    b[0] = 99;
    cout << a[0] << endl;
    cout << b[0] << endl;
    return 0;
}

Output
10
99

Explanation: In this, new memory is allocated for b and values are copied element-by-element, so changes in b do not affect a.

Example 2:

C++
#include <iostream>
using namespace std;

class Sample {
public:
    int *x;
    Sample(int val) {
        x = new int;
        *x = val;
    }
    // Deep Copy Constructor
    Sample(const Sample &obj) {
        x = new int;
        *x = *(obj.x);
    }
};

int main() {
    Sample obj1(10);      // Original object
    Sample obj2 = obj1;  // Deep copy

    *obj2.x = 50;        // Modify copied object

    cout << "obj1.x = " << *obj1.x << endl;
    cout << "obj2.x = " << *obj2.x << endl;

    return 0;
}

Output
obj1.x = 10
obj2.x = 50

Explanation:

  • "obj1" allocates memory and stores value 10.
  • custom copy constructor creates new memory for obj2.
  • "obj2" gets its own separate memory with the same value.
  • Changing obj2.x does not affect obj1.x.

Difference between the Shallow Copy and Deep Copy

 S.No.Shallow Copy Deep copy
1.When we create a copy of object by copying data of all member variables as it is, then it is called shallow copy When we create an object by copying data of another object along with the values of memory resources that reside outside the object, then it is called a deep copy
2.A shallow copy of an object copies all of the member field values. Deep copy is performed by implementing our own copy constructor.
3.In shallow copy, the two objects are not independentIt copies all fields, and makes copies of dynamically allocated memory pointed to by the fields
4.It also creates a copy of the dynamically allocated objectsIf we do not create the deep copy in a rightful way then the copy will point to the original, with disastrous consequences.
Comment