Shallow Copy and Deep Copy

7 views
Skip to first unread message

Shailendra shail

unread,
Jan 7, 2011, 2:04:49 AM1/7/11
to newidea_or...@googlegroups.com
What is Sallow Copy and Deep Copy ? If any one know please give suitable answer..with example.
Thanks

Amarnath Sundarababu

unread,
Jan 7, 2011, 2:28:25 AM1/7/11
to newidea_or...@googlegroups.com

Java provides a mechanism for creating copies of objects called cloning.

two ways ------ shallow copy and deep copy

 
Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the references are copied. Thus, if the object you are copying contains references to yet other objects, a shallow copy refers to the same sub objects.

 

Deep copy is a complete duplicate copy of an object. If an object has references to other objects, complete new copies of those objects are also made. A deep copy generates a copy not only of the primitive values of the original object, but copies of all subobjects as well, all the way to the bottom. If you need a true, complete copy of the original object, then you will need to implement a full deep copy for the object.

 

Shallow -> One new copy of object + required number of references copied

Deep -> One new copy of object + required number of new copies of objects are made for references in parent object

 

 

Thanks & Regards,

Amarnath

Harimohan Karunanithi

unread,
Jan 7, 2011, 2:34:41 AM1/7/11
to newidea_or...@googlegroups.com

Very Nice...

Deep Copy Sounds Peculiar..

Deep Copy  : Does It Copy the Entire Tree Structure an Object has..???

What about the Object Names,Not Object reference Name ???Will they be same..???

Nishant Gupta

unread,
Jan 7, 2011, 2:36:26 AM1/7/11
to newidea_or...@googlegroups.com
Shallow Vs Deep Copy.

A shallow of an object copies all of the member field values. This is ok if the fields are values, but may not be what you want for fields that point to dynamically allocated memory(pointers). The pointer will be copied. but the memory it points to will not be copied -- i.e the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want. The default copy constructor and assignment operator make shallow copies.
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator.

class String
{
     int   size;
     char* data;
};

String  s1("Ace");   // 1


In the above example, lets assume that the field s1.data was created at some memory location (10) .  If we do String s2 = s1, both s2.data, and s1.data will point to the "same" memory location i.e 10 which is not wrong.
But, if you write code, to actually allocate memory using the "new" operator, they will have different locations, and hence will be safe. For this, you'll have to write a custom Copy Constructor AND overload the Assignment Operator as well...

Cheers !

Thanks & Regards,

Nishant Gupta,
 APPLE - Developer (TCS Offshore)
neo324893 @ AIM
+91 9342420245, 9620779208

Nishant Gupta

unread,
Jan 7, 2011, 2:37:47 AM1/7/11
to newidea_or...@googlegroups.com
@Harimohan-> Read my explanation, I guess I've answered this there...

Thanks & Regards,

Nishant Gupta,
 APPLE - Developer (TCS Offshore)
neo324893 @ AIM
+91 9342420245, 9620779208

ankurguptajpr

unread,
Apr 24, 2014, 9:44:29 AM4/24/14
to newidea_or...@googlegroups.com
Can you suggest finnest way of doing deepcopy.
Few methods what I am aware of are:
     serialization 
     json 

If any other way to do deep clone than suggest even with these solution there are some cases when it breaks.
Reply all
Reply to author
Forward
0 new messages