GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Aug-2006, 04:32
vatsa_mitr vatsa_mitr is offline
New Member
 
Join Date: Apr 2006
Posts: 8
vatsa_mitr is on a distinguished road
Smile

deep copy and shallow copy


Hi ,
can anybody tell me the diff between deep copy and shallow copy.
  #2  
Old 14-Aug-2006, 07:19
Gamer_2k4's Avatar
Gamer_2k4 Gamer_2k4 is offline
Member
 
Join Date: Apr 2005
Location: Wisconsin
Posts: 117
Gamer_2k4 will become famous soon enough

Re: deep copy and shallow copy


I think (and someone please correct me if I'm wrong) that a shallow copy is when you create another reference to the same object.
CPP / C++ / C Code:
testObject * o1 = new testObject();
testObject * o2 = o1;
In the above case, o2 is a shallow copy of o1, because they both point to the same object.

Now a deep copy (again, someone please correct me if necessary) is when you reallocate the memory for an object, generally with a copy constructor.
CPP / C++ / C Code:
class testObject
{
public:
   testObject()
   {
      myVar = 0;
   }

   testObject(testObject &o)
   {
       myVar = o.myVar;
   }

   int myVar;
}

//elsewhere in the program
testObject * o1 = new testObject();
testObject * o2 = new testObject(*o1);

Here o2 is a deep copy of o1, because the memory is reallocated for the object. Even though they contain the same information, o1 and o2 are different objects.

Gamer_2k4
  #3  
Old 14-Aug-2006, 07:52
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: deep copy and shallow copy


Nice explanation Gamer. There's another twist to this, one that's more dangerous in code. Suppose the class testObject contains a c-style string, in other words a dynamically allocated char array.
CPP / C++ / C Code:
class testObject
{
    public:
        testObject(char *str) { m_str = new char[strlen(str)+1]; strcpy(m_str, str); }
        ~testObject() { if (m_str) delete[] m_str; }
    private:
        char *m_str;
};

int main()
{
    testObject o1("hello world");
    testObject o2 = o1; //use default copy constructor
    return 0; //when o1 and o2 go out of scope, their destructors will be called automatically
}

This program crashes as it terminates because of a double delete. Since no copy constructor is provided in testObject, the compiler creates a default one, which simply copies over the values of all the member variables. This is a shallow copy. The problem is, with the shallow copy o1.m_str == o2.m_str; in other words the two pointers point to the same place in memory. Then when they go out of scope, a destructor is called and deletes that memory. Then -- whoops -- another destructor is called and deletes the same memory. Crash.

The solution to this is to provide a copy constructor for testObject. (To be safe, there's also an assignment overload operator that does the same thing. See explanation below.)
CPP / C++ / C Code:
class testObject
{
    public:
        testObject(char *str) { m_str = new char[strlen(str)+1]; strcpy(m_str, str); }
        testObject(testObject& orig) { m_str = new char[strlen(orig.m_str)+1]; strcpy(m_str, orig.m_str); }
        ~testObject() { if (m_str) delete[] m_str; }
        testObject& operator=(testObject& other) { if (m_str) { delete[] m_str; } m_str = new char[strlen(other.m_str)+1]; strcpy(m_str, other.m_str); }
    private:
        char *m_str;
};

int main()
{
    testObject o1("hello world");
    testObject o2 = o1; //use the copy constructor I wrote for testObject (a constructor with one argument, unless declared with the explicit keyword, can be called with this syntax, so this is not assignment; rather it is equivalent to testObject o2(o1);
    testObject o3("three");
    o3 = o1; //use assignment operator
    return 0; //when o1, o2 and o3 go out of scope, their destructors will be called automatically
}
Now all three destructors will get called, but all three class instances own copies of the string "hello world" in different places in memory, so they can all delete their own strings and not crash. The copy constructor and assignment operator do deep copies.
  #4  
Old 14-Aug-2006, 09:12
Tomb332's Avatar
Tomb332 Tomb332 is offline
Junior Member
 
Join Date: Jul 2006
Location: Everywhere and yet nowhere at all
Posts: 47
Tomb332 is on a distinguished road

Re: deep copy and shallow copy


thats right
The proper definition as i know it is

Shallow copy
Just copies the member data nothing else (big problems if there is
allocated memory)(the default copy constructor uses shallow copies)

Deep copy
Copies everything and if there is allocated memory allocates
itself some memory for the data (insted of just copying the pointer
data (ends up with 2 pointers to same place as ubergeek said))
__________________
Forgive my english
(I am english i just can't spell)
PS:What do you think of the avatar, i drew parts myself
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:21.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.