WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Intermediate · question 23 of 100

What is the difference between a deep copy and a shallow copy? When would you use each?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

In C++, when an object is copied, there are two ways to do it: shallow copy and deep copy. The difference between the two is in how the memory is allocated and how the contents of the copied object are treated.

A shallow copy simply copies the values of the data members from one object to another, without creating new memory for the copied object. This means that both the original and the copied objects will share the same memory space for the data members. If any changes are made to the data members of the copied object, they will affect the original object as well.

Here’s an example of a shallow copy:

    #include <iostream>
    using namespace std;
    
    class ShallowCopy {
        public:
        ShallowCopy(int n) {
            data = new int;
            *data = n;
        }
        ShallowCopy(const ShallowCopy& other) {
            data = other.data;
        }
        void printData() {
            cout << "Data value: " << *data << endl;
        }
        private:
        int* data;
    };
    
    int main() {
        ShallowCopy obj1(5);
        ShallowCopy obj2 = obj1; // shallow copy
        obj1.printData();
        obj2.printData();
        obj2.data = new int;
        *obj2.data = 10;
        obj1.printData();
        obj2.printData();
        return 0;
    }

In this example, we create a class called ShallowCopy with a single integer data member called ’data’. The class has two constructors, one to initialize the data member with a value, and another to copy an object using a shallow copy. The printData() function simply prints the value of ’data’.

In the main() function, we create two objects of the ShallowCopy class, obj1 and obj2. We then perform a shallow copy of obj1 into obj2, which simply copies the memory address of the data member. We then print the value of ’data’ for both objects using the printData() function. We then modify the ’data’ member of obj2 by allocating new memory and setting the value to 10. Finally, we print the value of ’data’ for both objects again to show that the change to obj2 affected obj1 as well.

A deep copy, on the other hand, creates a new memory space for the copied object, and copies the values of the data members to this new memory space. This means that any changes made to the data members of the copied object will not affect the original object.

Here’s an example of a deep copy:

    #include <iostream>
    using namespace std;
    
    class DeepCopy {
        public:
        DeepCopy(int n) {
            data = new int;
            *data = n;
        }
        DeepCopy(const DeepCopy& other) {
            data = new int;
            *data = *(other.data);
        }
        void printData() {
            cout << "Data value: " << *data << endl;
        }
        private:
        int* data;
    };
    
    int main() {
        DeepCopy obj1(5);
        DeepCopy obj2 = obj1; // deep copy
        obj1.printData();
        obj2.printData();
        obj2.data = new int;
        *obj2.data = 10;
        obj1.printData();
        obj2.printData();
        return 0;
    }

In this example, we create a class called DeepCopy that is similar to the ShallowCopy class, but the copy constructor performs a deep copy. Instead of simply copying the memory address of the data member, the copy constructor allocates new memory for the data member and copies the value from the original object. The printData() function is the same as before.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics