473,749 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deep versus Shallow Copy - Part 1

I have decided (see earlier post) to paste my Word doc here so that it
will be simpler for people to provide feedback (by directly inserting
their comments in the post). I will post it in 3 parts to make it more
manageable.

Below is a draft of a document that I plan to give to my introductory
C++ class.

Please note that I have purposely left out any mention of safety issues
in the ctors which could be resolved thru some combination smart
pointers or exception handling. These topics will be introduced in the
follow up course.

If anyone would like the complete document in MS Word (after I have
incorporated improvements you folks suggest), send me an e-mail with
"Deep versus Shallow Copy" as a title. The appearance in the Word doc
is much better (this tool is rather limited when it comes to formatting
choices and it seems to get the indentation wrong when translating from
Word).

PART 1:

Subject: Explanation of Deep versus Shallow Copy
Author: Bob Langelaan
Date: Nov. 25th, 2006

Note: The reader should already have studied the following C++
concepts in order to be able to understand this document: pointers and
references; the "new" and "delete" operators; the member
initialization list (MIL); the "this" pointer.

1.0 Introduction

Let us start with the following class:

class Example1
{
private:
int ii;
double dd;
ABC abc;
};

Now let us instantiate several Example1 objects:

Example1 xyz1, xyz2;
..
.. // Assume xyz1's value modified here
..
xyz2 = xyz1; // Is this allowed?

Yes, the above program statement is allowed in C++. The assignment
operator is normally automatically overloaded (see section "3.0
Advanced Topic Addendum" below for details on some exceptions) for
any class in C++. If the programmer does not overload the assignment
operator, the compiler will do it for you. Let us assume that in this
case the programmer has not explicitly overloaded the assignment
operator. What will the compiler supplied assignment operator
(sometimes called the "implicit" or "synthesize d" assignment
operator) do in the statement above?

The ii member of xyz1 will be assigned to the ii member of xyz2; the dd
member of xyz1 will be assigned to the dd member of xyz2; and the abc
member of xyz1 will be assigned to the abc member of xyz2. This
process of assigning the members of one object to the members of
another object is called a "memberwise " assignment. Programmers
may also refer to this as a "shallow copy".

What if we have the following C++ statement?

Example1 xyz3 = xyz1; // Does this invoke the overloaded
// assignment operator as well?

No. In this case the copy constructor of the Example1 class will be
invoked. Just as with the overloaded assignment operator, if the
programmer does not supply a copy constructor for their class, the
compiler will. The compiler supplied copy constructor (the implicit or
synthesized copy constructor) does the equivalent to initializing all
of the members of the new object in its member initialization list
(MIL). This, in turn, will cause the copy constructor for each of the
members to be invoked. Therefore, in the previous C++ statement, the
compiler supplied copy constructor will effectively invoke the copy
constructor for each of the members of xyz3, passing to each copy
constructor the corresponding member of xyz1.

But what if we having the following class:

class Example2
{
public:
Example2();
private:
int ii;
double dd;
ABC * abcPtr; // will point to a dynamically created ABC object
};

And here is the implementation of the Example2 constructor:

Example2::Examp le2()
{
abcPtr = new ABC; // dynamically create an ABC object
// and have the class member abcPtr point to it.
Nov 26 '06 #1
13 5018
On 2006-11-26 19:39, blangela wrote:
What if we have the following?

Example2 xyz3 = xyz1; // Can we use the compiler
// supplied copy constructor here?

No. The compiler supplied copy constructor will do a shallow copy. It
will effectively do this:

xyz3.abcPtr = xyz1.abcPtr; // this simply assigns one pointer
// to the other pointer

when what we need it to effectively do is:

xyz3.abcPtr = new ABC; // first dynamically create an ABC object
// (remember that xyz3 is constructed in this copy constructor)
*(xyz3.abcPtr) = *(xyz1.abcPtr); // then do the same as the
// programmer supplied assignment operator - a deep copy

Unless you have some pedagogical reason not to wouldn't it be better
with something like this:

xyz3.abcPtr = new ABC(*(xyz1.abcP tr)); // Copy-create a new ABC object

Of course, since you expect the reader to be familiar with
initialization, it would be even better to use initialization in the
examples:

Example2::Examp le2(Example2& e)
: abcPtr(new ABS(*(e.abcPtr) )
{
}

It's a bit late here so there are probably some errors in the above but
I think you'll get the idea.

--
Erik Wikström
Nov 26 '06 #2

Erik Wikström wrote:
On 2006-11-26 19:39, blangela wrote:
What if we have the following?

Example2 xyz3 = xyz1; // Can we use the compiler
// supplied copy constructor here?

No. The compiler supplied copy constructor will do a shallow copy. It
will effectively do this:

xyz3.abcPtr = xyz1.abcPtr; // this simply assigns one pointer
// to the other pointer

when what we need it to effectively do is:

xyz3.abcPtr = new ABC; // first dynamically create an ABC object
// (remember that xyz3 is constructed in this copy constructor)
*(xyz3.abcPtr) = *(xyz1.abcPtr); // then do the same as the
// programmer supplied assignment operator - a deep copy


Unless you have some pedagogical reason not to wouldn't it be better
with something like this:

xyz3.abcPtr = new ABC(*(xyz1.abcP tr)); // Copy-create a new ABC object

Of course, since you expect the reader to be familiar with
initialization, it would be even better to use initialization in the
examples:

Example2::Examp le2(Example2& e)
: abcPtr(new ABS(*(e.abcPtr) )
{
}

It's a bit late here so there are probably some errors in the above but
I think you'll get the idea.

--
Erik Wikström
My students are still getting comfortable with pointers, MILs and the
new operator, so doing it the way I have shown makes the code easier to
understand. Otherwise, I agree with your suggestions.

Bob

Nov 26 '06 #3
blangela wrote:
I have decided (see earlier post) to paste my Word doc here so that it
will be simpler for people to provide feedback (by directly inserting
their comments in the post). I will post it in 3 parts to make it more
manageable.

Below is a draft of a document that I plan to give to my introductory
C++ class.

Please note that I have purposely left out any mention of safety issues
in the ctors which could be resolved thru some combination smart
pointers or exception handling. These topics will be introduced in the
follow up course.

If anyone would like the complete document in MS Word (after I have
incorporated improvements you folks suggest), send me an e-mail with
"Deep versus Shallow Copy" as a title. The appearance in the Word doc
is much better (this tool is rather limited when it comes to formatting
choices and it seems to get the indentation wrong when translating from
Word).

PART 1:

Subject: Explanation of Deep versus Shallow Copy
Author: Bob Langelaan
Date: Nov. 25th, 2006

Note: The reader should already have studied the following C++
concepts in order to be able to understand this document: pointers and
references; the "new" and "delete" operators; the member
initialization list (MIL); the "this" pointer.

1.0 Introduction

Let us start with the following class:

class Example1
{
private:
int ii;
double dd;
ABC abc;
};
I think the paper would be improved by using a more meaningful class
than "Example1" as the example. After all, if the example appears to be
contrived, then the student may question the relevancy of the material.
So the more "realistic" the example provided, the more likely that a
student will be able to apply it to C++ code they see and to the C++
code that they write. The specific class chosen does not matter all
that much: a "Shape" class, or a "Vehicle" class, or even a "String"
class are some of the usual choices.

Another important programming lesson is to choose the names of
identifiers carefully. Well-chosen names document the program and
minimize the likelihood of making a mistake. For that reason, this
program is not setting a good example by having identifiers with names
that differ only by one character at the very end of the name.
"Example1" and "Example2" (besides not communicating what either class
actually represents) may suggest that class names should resemble each
other as much as possible - when in fact the goal is the opposite.
Because the last thing that a programmer wants is to have two classes
whose names are easy to mix up.

Greg

Nov 27 '06 #4

Greg wrote:
blangela wrote:
I have decided (see earlier post) to paste my Word doc here so that it
will be simpler for people to provide feedback (by directly inserting
their comments in the post). I will post it in 3 parts to make it more
manageable.

Below is a draft of a document that I plan to give to my introductory
C++ class.

Please note that I have purposely left out any mention of safety issues
in the ctors which could be resolved thru some combination smart
pointers or exception handling. These topics will be introduced in the
follow up course.

If anyone would like the complete document in MS Word (after I have
incorporated improvements you folks suggest), send me an e-mail with
"Deep versus Shallow Copy" as a title. The appearance in the Word doc
is much better (this tool is rather limited when it comes to formatting
choices and it seems to get the indentation wrong when translating from
Word).

PART 1:

Subject: Explanation of Deep versus Shallow Copy
Author: Bob Langelaan
Date: Nov. 25th, 2006

Note: The reader should already have studied the following C++
concepts in order to be able to understand this document: pointers and
references; the "new" and "delete" operators; the member
initialization list (MIL); the "this" pointer.

1.0 Introduction

Let us start with the following class:

class Example1
{
private:
int ii;
double dd;
ABC abc;
};

I think the paper would be improved by using a more meaningful class
than "Example1" as the example. After all, if the example appears to be
contrived, then the student may question the relevancy of the material.
So the more "realistic" the example provided, the more likely that a
student will be able to apply it to C++ code they see and to the C++
code that they write. The specific class chosen does not matter all
that much: a "Shape" class, or a "Vehicle" class, or even a "String"
class are some of the usual choices.

Another important programming lesson is to choose the names of
identifiers carefully. Well-chosen names document the program and
minimize the likelihood of making a mistake. For that reason, this
program is not setting a good example by having identifiers with names
that differ only by one character at the very end of the name.
"Example1" and "Example2" (besides not communicating what either class
actually represents) may suggest that class names should resemble each
other as much as possible - when in fact the goal is the opposite.
Because the last thing that a programmer wants is to have two classes
whose names are easy to mix up.

Greg
Thanks for your input - Bob.

Nov 27 '06 #5
blangela wrote:
Below is a draft of a document that I plan to give to my introductory
C++ class.

Subject: Explanation of Deep versus Shallow Copy
Personally, I find the terms "shallow copy" and "deep copy"
confusing w.r.t. C++, and I'm glad my learning resources
never mentioned them.

The reality is that when you copy an object using the
default assignment operator or copy constructor, you get
an exact replica of the original. If the original had a handle
on a resource (eg. pointer to memory) then the copy will
have another handle on that same resource.

This is a very simple concept (to me, anyway) and does
not need to be obfuscated with discussions about "deep"
and "shallow". The term "shallow copy" can imply that
if an object contains another object, then the sub-object
won't be copied -- which is not true.

If it were me teaching, the point I would be making is
that if you want to allocate new resources when
copying an object, you are going to need a user-defined
assignment operator & copy constructor.

BTW I don't think it's correct to talk about the default
assignment operator as being an "overload" -- not sure
on that though.

Nov 27 '06 #6
Old Wolf <ol*****@inspir e.net.nzwrote:
>Personally, I find the terms "shallow copy" and "deep copy"
confusing w.r.t. C++, and I'm glad my learning resources
never mentioned them.
>The reality is that when you copy an object using the
default assignment operator or copy constructor, you get
an exact replica of the original. If the original had a handle
on a resource (eg. pointer to memory) then the copy will
have another handle on that same resource.
>This is a very simple concept (to me, anyway) and does
not need to be obfuscated with discussions about "deep"
and "shallow". The term "shallow copy" can imply that
if an object contains another object, then the sub-object
won't be copied -- which is not true.
I totally agree. "Shallow copy" means the same thing as
simply "copy". "Deep copy" means you have an algorithm that
descends (at least some) indirect references and makes
duplicates. "Deep copies" involve procedural code that
is situation specific, and saying "this makes a deep copy"
is in general ambiguous.

Steve
Nov 27 '06 #7

Steve Pope wrote:
Old Wolf <ol*****@inspir e.net.nzwrote:
Personally, I find the terms "shallow copy" and "deep copy"
confusing w.r.t. C++, and I'm glad my learning resources
never mentioned them.
The reality is that when you copy an object using the
default assignment operator or copy constructor, you get
an exact replica of the original. If the original had a handle
on a resource (eg. pointer to memory) then the copy will
have another handle on that same resource.
This is a very simple concept (to me, anyway) and does
not need to be obfuscated with discussions about "deep"
and "shallow". The term "shallow copy" can imply that
if an object contains another object, then the sub-object
won't be copied -- which is not true.

I totally agree. "Shallow copy" means the same thing as
simply "copy". "Deep copy" means you have an algorithm that
descends (at least some) indirect references and makes
duplicates. "Deep copies" involve procedural code that
is situation specific, and saying "this makes a deep copy"
is in general ambiguous.

Steve
A shallow copy is a member-wise copy. A deep copy is anything other
than a shallow copy. What could be simpler than that?

Nov 27 '06 #8
blangela <Bo***********@ telus.netwrote:
>Steve Pope wrote:
>Old Wolf <ol*****@inspir e.net.nzwrote:
>Personally, I find the terms "shallow copy" and "deep copy"
confusing w.r.t. C++, and I'm glad my learning resources
never mentioned them.
>The reality is that when you copy an object using the
default assignment operator or copy constructor, you get
an exact replica of the original. If the original had a handle
on a resource (eg. pointer to memory) then the copy will
have another handle on that same resource.
>This is a very simple concept (to me, anyway) and does
not need to be obfuscated with discussions about "deep"
and "shallow". The term "shallow copy" can imply that
if an object contains another object, then the sub-object
won't be copied -- which is not true.
>I totally agree. "Shallow copy" means the same thing as
simply "copy". "Deep copy" means you have an algorithm that
descends (at least some) indirect references and makes
duplicates. "Deep copies" involve procedural code that
is situation specific, and saying "this makes a deep copy"
is in general ambiguous.
>A shallow copy is a member-wise copy. A deep copy is anything other
than a shallow copy. What could be simpler than that?
It's simple, but it also makes the definition of "deep copy"
so broad as to be fairly useless.

Steve
Nov 27 '06 #9
blangela wrote:
>
A shallow copy is a member-wise copy. A deep copy is anything other
than a shallow copy. What could be simpler than that?
Firstly, if that is your definition then why not just call them
what they are: "member-wise copy" and "non member-wise copy" ?

Second, that's not the commonly accepted definition of
'shallow copy' and 'deep copy' in computer science, so
it is confusing. Look at this example, where Foo is some class:

// C++:
class S { public: Foo foo; };

// Java:
class S { public Foo foo; };

In Java, assigning an object of type S to another object of type S
results in both objects having a handle on a single shared instance
of Foo. I think we would agree that this is a shallow copy.

In C++, the same assignment results in both objects having their
own Foo objects (so there are now two Foos in existence).

Now, you are trying to say that the C++ behaviour is a "shallow copy".
It is clearly different from the Java behaviour which is a shallow
copy.

I think this shows that it is mistaken to call the C++ member-wise
copy a "shallow copy".

Nov 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3287
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the assignment Student* john = michael; both john and michael share the same object. This type of an assignment is different then value-assignmnet semantics used by class variables, as in
2
12350
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
4
52304
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx) 10.What's the difference between the System.Array.CopyTo() and System.Array.Clone()?
2
10120
by: bonk | last post by:
I have come across the need to distinguish between the creation of a deep and a shallow copy and with great interest I have read this article: http://blogs.msdn.com/brada/archive/2004/05/03/125427.aspx This artivle seems to hint that I should not use System.IClonable but instead define my own interface(s) for cloning. Now since this article is rather old and since they did not obsolete IClonable there might be a new "best practise".
26
15804
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
9
1675
by: blangela | last post by:
2.0 Sample Code class ABC // dummy class used below {}; class Example2 { public: Example2(); // default ctor Example2( const Example2 &); // copy ctor
1
2123
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a reference member, the compiler will not be able to synthesize an assignment operator for your class. It actually helps to think of a reference member as a const member (since it cannot be made to reference any other object once it has been initialized). ...
4
3148
by: shuisheng | last post by:
Dear All, Is there any easy way to make sure all my object copies are deep copy or shallow copy? I do not like to implement it in each class one by one. Thanks, Shuisheng
3
8749
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. ...
0
8997
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
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
9389
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
9256
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
8257
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
6801
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
4709
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
3320
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
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.