473,799 Members | 3,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deep copying

1 New Member
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 1618
JazibBahir
2 New Member
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 New Member
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.number s and example2.number s 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
4535
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 assignment operator? So far I can't find much useful in the regular documentation. I understand the basic concept that names are bound to objects which exist on the heap, but that still doesn't explain why a = b doesn't _always_ cause a to point to the...
0
1255
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 fast deep-copying of instances (the cache is used heavily). i tried to use copy module but it's *very* slow, then i tried cPickle to pickle and depickle instances which was faster, but still slow.
1
2324
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 / method should ensure that an Integer_Field is not copied (assigned) to a String_Field or other descendant of Field. I have looked at the Visitor design pattern with
5
3120
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 everything copied, public, private, internal, events, etc... Thanks, Steve
1
984
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. -- Jeff S.
4
3039
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 defined in the XSD. I deserialize XML data into instances of that class, and serialize back to XML. So far so good. However, in my code I have the concept of wanting to create new copies of these deserialized objects and modifying some of...
6
4013
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
2824
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 to go for deep copying? Thanks in advance.
3
8752
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 format, 3 pp) My question, coming from a C++ background where deep copying is done, is why in C# you would do either deep or shallow copying as suggested by O'Reilly (using the "ICloneable" inhereited interface), at least for the .NET framework. ...
3
2459
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 it contains pointers. I would have liked to think I could copy construct it, but I can't. I need to follow the pointers. Does anybody know if there's a quick and easy way to achieve this (as opposed to manually following the pointers and fields...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10252
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.