Shallow copy Vs Deep copy

In this blog we will study the difference between shallow and deep copy in c++. This is very basic concept in c++ and here we will learn how to copy an object in c++ in correct way. I had an interview in c++ and the very first question he asked to me by giving two different simple classes, that whether i need to provide my own copy constructor or not.  for example

deep copy and shallow copy

 Based on above class A and class B , which class requires own copy constructor and overloaded assignment operator?

The class A does not require copy constructor and overloaded assignment operator while class B requires copy constructor and overloaded assignment operator. Why? When we do not provide our own copy constructor or assignment operator, they are provided by the compiler. But these copy constructor or assignment operator  provided by compiler do only shallow copy.

what do you mean by deep copy and shallow copy in c++?

A shallow copy of an object, copies the values of all the data members into another abject of same class. When members are dynamically allocated, the built-in copy constructor and operator= are not going to be enough. In case of a class having dynamically allocated members like below abc class.

class abc {
public:
    class( const char* str );
    ~class();
private:
    char* stored;
};

We have to provide three things for class abc.

1. Write a proper copy constructor.
2. Overload the assignment operator.
3. Write a destructor that frees the memory.

When we provide above three things to avoid memory leaks or undefined behavior, this operation is called deep copy.

Shallow copy example with issue:

#include<iostream>
#include <string.h>
using namespace std;

class MyName
{
public:
  MyName (const char *str);
   ~MyName ();
private:
  char *emp_name;
};

MyName::MyName (const char *str)
{
  emp_name = new char[strlen (str) + 1];
  strcpy (emp_name, str);
}

MyName::~MyName ()
{
  delete[]emp_name;
}

int main ()
{
  MyName name ("wikistack");
  MyName name1 = name;		// initialie name1 from name object
  return 0;
}

 

Here we will have issue after running the above code. Because we have not provided copy constructor and overloaded assignment operator in class MyName, in this compiler will generate a copy constructor for class MyName and the generated copy consturctor will only do member wise copy of data members. In  member wise copying only pointer will be copied, the buffer of char* emp_name will not be copied.

In this case when the object name will go out of scope ,its destructor will be called and delete[] emp_name will get succeeded. But when object name1 will go out of scope its destructor will be called and delete[] emp_name will run into undefined behavior. most probably it will give memory corruption like below.

*** Error in `./a.out’: double free or corruption (fasttop): 0x0000000001bf7010 ***
Aborted (core dumped)

Note: so we have to provide deep copy for class like above example to avoid these types of run time errors. we have to provide copy constructor and assignment operator like below code.

Deep copy example:

#include<iostream>
#include <string.h>
using namespace std;

class MyName
{
public:
  MyName (const char *str);
  ~MyName ();
  MyName (const MyName & another);
  void operator = (const MyName & another);
private:
  char *emp_name;
};

MyName::MyName (const char *str)
{
  emp_name = new char[strlen (str) + 1];
  strcpy (emp_name, str);
}

MyName::~MyName ()
{
  delete[]emp_name;
}

MyName::MyName (const MyName & another)
{
  emp_name = new char[strlen (another.emp_name) + 1];
  strcpy (emp_name, another.emp_name);
}

void
MyName::operator = (const MyName & another)
{
  char *temp = new char[strlen (another.emp_name) + 1];
  strcpy (temp, another.emp_name);
  delete[]emp_name;
  emp_name = temp;
}

int main ()
{
  MyName name ("wikistack");
  MyName name1 = name;
  return 0;
}

 

Reference:

http://nadimahmad.blogspot.in/2011/08/shallow-copy-and-deep-copy-in-c.html
https://www.cs.umd.edu/class/winter2012/cmsc389C/Slides/Winter12Lec07.pdf



Related Contents to follow