ambuli
asked on
Deep Copy vs Shallow copy
Hi Experts,
Could you please give me a quick simple example of deep copy vs shallow copy.
Thank you.
Could you please give me a quick simple example of deep copy vs shallow copy.
Thank you.
ASKER
Thanks Jkr. Sorry, I wasn't clear enough. I just want to understand how we eliminate shallow copy using a copy constructor. I read that the default copy constructor does a shallow copy.
Well, in that case, maybe the example should be a bit more complex, e.g.
class A
{
// members
string s;
vector<string> vs;
A& assign(const A& a) // helper to be used by both the copy ctor and assignment operator
{
// copy each member
s = a.s;
vs = a.vs;
}
public:
A(const A& a) // copy constructor
{
assign(a);
}
A& operator=(const A& a) // assignment operator
{
return assign(a);
}
};
For example, you have a pointer in a structure(or class)
struct node
{
char* ptr;
};
In copy constructor, if you copy the ptr itself, it's a shallow copy. If you allocate a new memory, copy the content of the string, that's a deep copy.
struct node
{
char* ptr;
};
In copy constructor, if you copy the ptr itself, it's a shallow copy. If you allocate a new memory, copy the content of the string, that's a deep copy.
The ShallowCopy and DeepCopy classes differ in that the DeepCopy class contains a copy constructor. Here is the output when running this program:
*sc.pb 43 *sc2.pb 43
*dc.pb 22 *dc2.pb 44
Notice that when we execute the line:
*sc2.pb = 43;
then *sc.pb has the same value. If this is desired, then fine. But if you require that the value pointed to by sc.pb be maintained independent of operations on sc2, then a deep copy is in order. The DeepCopy class copy constructor allocates its own memory for pb, and changing one object of DeepCopy does not affect the value in another object of DeepCopy.
*sc.pb 43 *sc2.pb 43
*dc.pb 22 *dc2.pb 44
Notice that when we execute the line:
*sc2.pb = 43;
then *sc.pb has the same value. If this is desired, then fine. But if you require that the value pointed to by sc.pb be maintained independent of operations on sc2, then a deep copy is in order. The DeepCopy class copy constructor allocates its own memory for pb, and changing one object of DeepCopy does not affect the value in another object of DeepCopy.
#include <iostream>
using namespace std;
class ShallowCopy {
public:
ShallowCopy( int aa, int* pp): a(aa), pb(pp) {}
int a;
int * pb;
};
class DeepCopy{
public:
DeepCopy( int aa, int* pp): a(aa), pb(pp) {}
DeepCopy( const DeepCopy &d ) {
this->a = d.a;
this->pb = new int(*d.pb);
}
int a;
int * pb;
};
void main() {
int * pIntDeep = new int;
int * pIntShallow = new int;
*pIntShallow = 22;
*pIntDeep = 22;
ShallowCopy sc( 2, pIntShallow );
ShallowCopy sc2(sc);
*sc2.pb = 43;
DeepCopy dc( 1, pIntDeep );
DeepCopy dc2(dc);
*dc2.pb = 44;
cout << " *sc.pb " << *sc.pb << " *sc2.pb " << *sc2.pb << endl;
cout << " *dc.pb " << *dc.pb << " *dc2.pb " << *dc2.pb << endl;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
class A
{
string s;
};
A a;
A b;
a=b; //deep copy
memcpy(&a,&b,sizeof(A)); // shallow copy
Also check out http://www.devx.com/tips/Tip/13625 ("Deep Copy and Shallow Copy" - that's where the example comes from) and http://en.wikipedia.org/wiki/Object_copy for further information