473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Deep copying

Have read on many blogs and sites regarding copyto doing deep copying and clone doing shallow one... when done simple example found both gave same result so what exactly deep copying means? is it truly achievable using copyto?
Feb 1 '12 #1
2 1610
Deep Copy
A deep copies all field and make copies of dynamically allocated memory pointed to by the field.A deep copy occurs when an object is copy along with the object to it refer.
Shallow Copy
Shallow copy is a bit wise copy an object.A new object is created that has an exact copy of the value in the original object.If any of the field of the object are reference to other object,just the reference address are copied.
Feb 4 '12 #2
Falkeli
19
To make it clearer:

Let's assume we have the following class:
Expand|Select|Wrap|Line Numbers
  1. class Example
  2. {
  3.     private int[] numbers;
  4.     private StringBuilder builder;
  5.  
  6.     public Example()
  7.     {
  8.         numbers = new int[20];
  9.         builder=  new StringBuilder();
  10.     }
  11.  
  12.     // And other methods
  13. }
  14.  
Let example1 be an object of type Example; example2 be a shallow copy of it; and example3 be a deep copy.

Now, if you do the following code:
Expand|Select|Wrap|Line Numbers
  1.     example1.numbers[4]=100;
  2.     example2.numbers[4]=50;
  3.     example3.numbers[4]=20;
  4.     Console.Writeln("1: {0}; 2: {1}; 3: {2}", example1.numbers[4], example2.numbers[4], example3.numbers[4]);
  5.  
The output will be:
1: 50; 2: 50; 3: 20

This is because example1.numbers and example2.numbers are the same array, since example2 is a shallow copy; example is a different array, since it's a deep copy.
Feb 12 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: dan | last post by:
without stirring the pot too much -- could someone please point me to whatever documentation exists on the philosophy, semantics, and practical implications of how Python implements the...
0
by: Michal Vitecek | last post by:
hello, i have a data cache which sits between a slow database and my server and it's used to cache data retrieved from the database. in order for it to work correctly (race-conditions) i need a...
1
by: Thomas Matthews | last post by:
Hi, I'm looking for an efficient method to deep copy containers of fields. A Field is a parent class with children such as Integer_Field, String_Field, and Date_Field, etc. The algorithm /...
5
by: Steve - DND | last post by:
Is there any way to easily deep copy an entire object? Maybe something with unsafe code to make a full and completely duplicated copy of an object with no reference ties to the original? I want...
1
by: Jeff Stewart | last post by:
Does the .NET Framework intrinsically support any kind of deep copying for collections? All the documentation I read mentions deep copying, but never offers any method for performing it. --...
4
by: jjkboswell | last post by:
I have an XSD which I have generated a class from using the xsd.exe tool. My XSD contains complex types within it, so that the generated class has member variables which are of types that are also...
6
by: AllenGnr | last post by:
There are two objects, instants of two different class, but they has same schema. Class ClientDivision { public string Name { get {}; set {}; }
2
bajajv
by: bajajv | last post by:
Hi, I am confused regarding these concepts. Please clearify. 1. Why is bitwise copying unsafe? 2. Does copy constructor does bitwise copying? 3. Is the main use of explicit copy constructor is...
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
3
by: bob | last post by:
hi, I have been tasked with transferring a rather complex, deeply nested structure across dll's / memory spaces and effectively I need to deep copy the structure in question. Its a C struct and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.