Questions tagged [shallow-copy]

A shallow copy of an object is a duplicate that may not be fully independent of the original, in the sense that any references / pointers it holds to other objects refer to the same objects that the original's do. Use this tag for questions regarding implementing or using shallow copying methods.

Shallow copying is a process by which an object is duplicated to create a copy that may not be wholly independent of the original. Also known as "field by field" copying, this differs from deep copying with respect to the treatment of references / pointers to other objects: shallow copying copies the references / pointers held by the original, whereas deep copying makes a deep copy of each referenced object, too. Shallow copying is "shallow" in the sense that it copies only the original object itself, not any appearing deeper in its reference graph.

Related tags

Links

394 questions
753
votes
31 answers

What is the difference between a deep copy and a shallow copy?

What is the difference between a deep copy and a shallow copy?
David Locke
  • 17,926
  • 9
  • 33
  • 53
205
votes
9 answers

How do I create a copy of an object in PHP?

It appears that in PHP objects are passed by reference. Even assignment operators do not appear to be creating a copy of the Object. Here's a simple, contrived proof: b = "after";…
Nick Stinemates
  • 41,511
  • 21
  • 59
  • 60
143
votes
7 answers

Why and when to use angular.copy? (Deep Copy)

I've been saving all the data received from services direct to local variable, controller, or scope. What I suppose would be considered a shallow copy, is that correct? Example: DataService.callFunction() .then(function(response) { $scope.example…
Superman2971
  • 1,505
  • 2
  • 11
  • 10
111
votes
3 answers

Shallow copy of a Map in Java

As I understand it, there are a couple of ways (maybe others as well) to create a shallow copy of a Map in Java: Map data = new HashMap(); Map shallowCopy; // first way shallowCopy = new…
dcp
  • 54,410
  • 22
  • 144
  • 164
68
votes
8 answers

Fastest Way to do Shallow Copy in C#

I wonder what is the fastest way to do shallow copying in C#? I only know there are 2 ways to do shallow copy: MemberwiseClone Copy each field one by one (manual) I found that (2) is faster than (1). I'm wondering if there's another way to do…
tep
  • 1,237
  • 2
  • 13
  • 13
68
votes
2 answers

How to clone or copy a set in Python?

For copying a list: shallow_copy_of_list = old_list[:]. For copying a dict: shallow_copy_of_dict = dict(old_dict). But for a set, I was worried that a similar thing wouldn't work, because saying new_set = set(old_set) would give a set of a set? But…
Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124
67
votes
9 answers

When should I pass or return a struct by value?

A struct can be either passed/returned by value or passed/returned by reference (via a pointer) in C. The general consensus seems to be that the former can be applied to small structs without penalty in most cases. See Is there any case for which…
Kaiting Chen
  • 1,076
  • 1
  • 7
  • 12
64
votes
7 answers

Does Object.assign() create a deep copy or a shallow copy?

I just came across this concept of var copy = Object.assign({}, originalObject); which creates a copy of original object into the "copy" object. However, my question is, does this way of cloning object create a deep copy or a shallow copy? PS: The…
ShivangiBilora
  • 2,912
  • 4
  • 20
  • 27
63
votes
11 answers

In Java, what is a shallow copy?

java.util.Calendar.clone() returns "...a new Calendar with the same properties" and returns "a shallow copy of this Calendar". This does not appear to be a shallow copy as answered here on SO. That question is tagged language-agnostic, Java does…
Will
  • 19,789
  • 10
  • 43
  • 45
51
votes
8 answers

Default assignment operator= in c++ is a shallow copy?

Just a simple quick question which I couldn't find a solid answer to anywhere else. Is the default operator= just a shallow copy of all the class' members on the right hand side? Class foo { public: int a, b, c; }; foo f1, f2; ... f1 =…
Christopher Dorian
  • 2,163
  • 5
  • 21
  • 25
43
votes
11 answers

Can I use memcpy in C++ to copy classes that have no pointers or virtual functions

Say I have a class, something like the following; class MyClass { public: MyClass(); int a,b,c; double x,y,z; }; #define PageSize 1000000 MyClass Array1[PageSize],Array2[PageSize]; If my class has not pointers or virtual methods, is it…
SmacL
  • 22,555
  • 12
  • 95
  • 149
38
votes
5 answers

Python list slice syntax used for no obvious reason

I occasionally see the list slice syntax used in Python code like this: newList = oldList[:] Surely this is just the same as: newList = oldList Or am I missing something?
Charles Anderson
  • 19,321
  • 13
  • 57
  • 73
38
votes
6 answers

Shallow copy or Deep copy?

I am a bit new to these two methods of copying one object into the other. I am confused and unable to spot out the major difference between deep copy and shallow copy.. I had gone through a lots of theory regarding this, but I need explanation with…
Srinivas Cheruku
  • 1,314
  • 3
  • 11
  • 19
36
votes
4 answers

Object copy using spread syntax actually shallow or deep?

I understand spread syntax makes a shallow copy of objects, i.e., the cloned object refers to the same reference as the original object. However, the actual behaviour seems contradicting and confusing. const oldObj = {a: {b: 10}}; const newObj =…
33
votes
3 answers

Shallow copy of a hashset

Whats the best way of doing it? var set2 = new HashSet(); Traverse the set with a foreach like this. foreach (var n in set) set2.Add(n); Or use something like union like this. set2 = set.UnionWith(set); // all the elements
alan2here
  • 3,223
  • 6
  • 37
  • 62
1
2 3
26 27