Is pass-by-value/reference equivalent to making a deep/shallow copy, respectively?
Asked Answered
A

4

15

To reword the question in case someone types it into the search bar differently: Is pass-by-value the same as making a deep copy, and is pass-by-reference the same as making a shallow copy?

If not, what is the difference? In Python, the language I'm most familiar with, they appear indistinguishable.

Anarthrous answered 12/7, 2017 at 19:8 Comment(0)
S
19

No. Those two things are completely unrelated.

Shallow copy/deep copy is talking about object copying; whereas pass-by-value/pass-by-reference is talking about the passing of variables.

In many modern languages, like Python (which you mentioned that you're most familiar with) and Java, "objects" are not values in the language, so "objects" cannot be assigned or passed. Rather, objects are always manipulated through pointers to objects (references), which are values and can be assigned or passed.

Python and Java are pass-by-value only. When you pass a reference, it copies the pointer and you end up with two pointers to the same object. No object copying happens. In these languages, object copying is not done through assigning or passing, but rather is done by calling special methods like .clone() or by passing an object to a constructor to construct a new object. (There is in fact no general way to copy an object in Java.)

There are some languages, like C++, where objects can be values (of course, you can also have pointers to objects, which work similarly to references in other languages). C also has both pass-by-value and pass-by-reference. If you pass an object by reference, no copying happens. If you assign or pass an object by value, the object is copied. But by default this is a shallow copy.

The distinction between shallow copy and deep copy is how they deal with members of the object which are pointers to another object. Members which are not pointers are simply copied; there is no concept of "shallow" or "deep". "Shallow" or "deep" only talks about members which are pointers -- whether to copy the object pointed to or not. The default assignment operator and copy constructor in C++ simply copies each member. For members that are pointers, that means the pointer is copied and thus you have two pointers to the same object. That is a shallow copy.

You might want a "deep" copy in cases where the member that is a pointer to another object is really pointing to a "sub-object" that is really "a part of" your object (whether a pointer to another object means a sub-object or not depends on the design of your objects), so you don't want multiple of your objects pointing to the same sub-object, and that's why you want to copy the sub-object when copying the main object. To implement this in C++, you would need to implement a custom assignment operator and custom copy constructor to copy the "sub-objects" in the process of copying. In other languages, you would similarly need to customize whatever copying method is used.

Sewole answered 28/7, 2017 at 7:45 Comment(2)
Is it really true that if you pass an object by value in C++ it is a shallow copy? I thought I read it would use the copy-constructor, which will usually create a deep copy. E.g. if you pass a vector or string by value, the data will be copied, and not just the length and pointer to data.Infrequency
@ThomasAhle: Well, vector and string implement custom copy constructors and assignment operators that copy the contents. What I said was that the default copy constructor and assignment operator would simply assign instance variables of pointer type, and not copy the thing that the pointers pointed to.Sewole
C
3

pass-by-value makes a shallow-copy of the object.

On the other side,

pass-by-reference does not make any copy, it gets the reference of the object itself by just renaming it, so no any copying operation.

Carbonari answered 9/7, 2020 at 15:5 Comment(0)
B
0

Since I'm here anyway.. from the docs...

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

So its about how you copy an object.. which of course contains references to other objects.

Do you just copy the immediate object (copy), or recursively copy all objects it holds references too (deepcopy).

Bayly answered 11/6, 2019 at 2:34 Comment(0)
H
-2

Yes, pass-by-value makes a deep copy and pass-by-reference makes a shallow copy

Holdfast answered 12/7, 2017 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.