answersLogoWhite

0


Best Answer

A "shallow" copy is when the member values are physically copied from one

object to another, *including* the values of any pointer or reference

members. If there are pointer or reference memebrs, then, those poointers

or references refer to the *same* objects as the original object, which is

usually a bad thing. That's why you want to define a copy constructor and

assignment operator for objects that contain pointers or references.

It's called a "shallow" copy because only the values of the

pointers/references are copied, instead of making copies of those

referred-to objects and setting pointers to them. *That* is what would be

called a "deep" copy, because it's going "deeper" into the structure,

copying everything, not just the first "layer".

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is difference between Deep copy and shallow copy?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is shallow copy in Java?

A shallow copy in Java refers to the copying of object references from one collection to another. In contrast, a deep copy in Java refers to the copying of actual object data from one collection to another. // shallow copy Object[] original = {new Object(), new Object(), new Object()}; Object[] copy = new Object[original.length]; for(int i = 0; i < original.length; ++i) copy[i] = original[i]; // deep copy Object[] original = {new Object(), new Object(), new Object()}; Object[] copy = new Object[original.length]; for(int i = 0; i < original.length; ++i) // this would not compile in this case because Object does not have a publicly // accessible clone() method. if you were to use this, make sure that the type // has an accessible clone() method copy[i] = original[i].clone();


The difference between upload and download in Computer language?

upload: send a copy from source (yours) to another destination, usually a different machine download: copy from another machine (or web site) to yours (pc, pda, etc) Not all computer language have this abstraction, not even copy.


What are the advantages of constructor and destructor?

Without a copy constructor the only way to copy an object would be to instantiate a new object (or use an existing object) and then assign another object's value to it. However, it would be very odd indeed to have a copy assignment operator without a matching copy constructor. If you have one, you must have both. If you do not need to copy an object of a particular class, however, you can simply delete both the copy constructor and the copy assigment operator for that class. Any attempt to copy or copy assign would then result in a compile-time error.


What is the difference between cut and a copy command?

a copy command would copy the contents of the selection and when you paste it somewhere else, the original contents would still be there. the contents would be available in both the original location and the new location a cut command would remove the contents of the selection and when you paste it somewhere else, the original contents would be lost. it would be available in the new location only.


Difference between asymmetric and symmetric multiprocessing and simple multiprocessors?

A symmetric multiprocessing (SMP) system is one in which each processor runs an identical copy of the operating system on each processor. In an asymmetric multiprocessing, system, each processor is assigned a specific tasks. An example of this would be a master- slave relationship between one processor and “the rest”.

Related questions

What is the difference in shallow copy and deep copy in mobile application development process?

Shallow copy also known as address copy involves copying of the address and not the actual data. In case of deep copy, the data is copied.


What is difference between Deep copy and shallow copy in iphone?

Shallow copy is duplicate little as possible. because of it copy one object to another object. it copy only structure not elements . Deep copy means Duplicate everything. it will be copy structure as well as elements; e.g :- char A*={'a','b','c'}; char B*={'x','y','z'}; B=A;


What is shallow copy in Java?

A shallow copy in Java refers to the copying of object references from one collection to another. In contrast, a deep copy in Java refers to the copying of actual object data from one collection to another. // shallow copy Object[] original = {new Object(), new Object(), new Object()}; Object[] copy = new Object[original.length]; for(int i = 0; i < original.length; ++i) copy[i] = original[i]; // deep copy Object[] original = {new Object(), new Object(), new Object()}; Object[] copy = new Object[original.length]; for(int i = 0; i < original.length; ++i) // this would not compile in this case because Object does not have a publicly // accessible clone() method. if you were to use this, make sure that the type // has an accessible clone() method copy[i] = original[i].clone();


What is a shallow copy in c plus plus?

Shallow copies occur whenever we copy "naked" pointers.int* p1 = new int {42};int* p2 = p1; // shallow copyWe call this a shallow copy because although p1 and p2 are independent pointer variables, they both refer to the same memory. If we change the value stored in that memory, we affect both pointers:assert (*p2 == 42);*p1 = 0;assert (*p2 == 42); // error!Side-effects such as this are completely at odds with how a "normal" copy behaves:int x = 42;int y = x;assert (y == 42);x = 0;assert (y == 42); // okHere, changing the value of one variable has no effect upon the other because they are completely independent variables.To mimic this behaviour with pointer variables we must perform a deep copy, copying the memory being referred to:int* p1 = new int {42}; int* p2 = new int {*p1}; // deep copyassert (*p2 == 42);*p1 = 0;assert (*p2 == 42); // okA shallow copy is (usually) cheaper than a deep copy, thus it is not unusual for class designers to postpone an expensive deep copy until it becomes absolutely necessary to do so (also known as a lazy copy). However, since C++11, the inclusion of move semantics and standard library smart pointers and resource handles has greatly reduced the need for shallow copying.


What is the difference between the Copy command and he Xcopy command?

d


What is the difference between copy con and copy in MS-DOS?

copy command is used to make a copy of file or copycon is used to write a file


What is difference between hard and soft copy of computer?

soft copy is nothing but file or image,savable copy to system, hard copy is nothing but physical copy


What is the difference between a hard copy and a document?

a hard copy is a printed out version of the document you are working on hope that helps :)


What actors and actresses appeared in Shallow Copy - 2009?

The cast of Shallow Copy - 2009 includes: Jolee Kim as Tabitha Demetrius Sager as Will Michael Thomas Moore as Max


What is the difference between a backup copy and a backing storage device?

wat is pckup


What does self attested copy mean?

What is difference between attested and self attested


What is the basic difference between copy and moving?

When you copy, the original is left where it is. When you move, the file is first copied, then deleted from the original location.