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
4
votes
1 answer

shallow-copy a segment of a value type array

I'm trying to shallow-copy a double[] into segments, and pass those segments to new threads, like so: for (int i = 0; i < threadsArray.Length; i++) { sub[i] = new double[4]; //Doesn't shallow copy since double is a value type …
Frayed
  • 193
  • 6
3
votes
3 answers

Create shallow copy/clone of subclass of EntityObject

We have an audit table in our database, and on update the old and new values are serialized to XML and stored in the same row. The objects are currently deep-cloned thus: public EntityObject CloneEntity(EntityObject obj) { …
sennett
  • 8,014
  • 9
  • 46
  • 69
3
votes
4 answers

Shallow Copy - Reference type anomalous nature

I cannot understand the output of the two sets of code snippets given below. How don't really get the concept of shallow copy. How can it be explained? Class: public class Person : ICloneable { public string Name; public int[] arr;…
Bijoy
3
votes
4 answers

Why is the JavaScript spread notation not working here

I am learning React and have this simple problem I can't solve. I created a Codesandbox Here in the image: the file is a key-value array like the image show. After this code below has run: return { ...file, md5: SHA256(fileContents).toString()…
Kid
  • 1,869
  • 3
  • 19
  • 48
3
votes
3 answers

Delete property from shallow copied object

If I have one object, and shallow copy of it. For example: var person = { name: "Foo", age: 10, child: { name: "Matrix" } } var copiedPerson = {...person} console.log(person, copiedPerson); If I change person.name = "NewName";,…
Predrag Davidovic
  • 1,411
  • 1
  • 17
  • 20
3
votes
1 answer

numpy - explanation of "np.copy is a shallow copy and will not copy object elements within arrays"

Background numpy.copy(a, order='K', subok=False)[source] says: Note that np.copy is a shallow copy and will not copy object elements within arrays. This is mainly important for arrays containing Python objects. The new array will contain the same…
mon
  • 18,789
  • 22
  • 112
  • 205
3
votes
2 answers

How to explicitely make a deep copy of an array in MATLAB?

For example, I want to do a deep copy of a to b: >> a=zeros(2,3); >> b=a; So here = creates only a shallow copy. My question is, how to generate a deep copy in this case? I know that I can add a command like b(1,1)=b(1,1) to make it a deep copy.…
f. c.
  • 1,095
  • 11
  • 26
3
votes
1 answer

Why does shallow copy behaves as deep copy for a simple list

I was going understanding shallow copy and deep copy concepts in python. I observe most of the posts/blogs/SO answer explain these concepts are using a nested lists. import copy lst = [[1,2,3],[4,5,6]] b = copy.copy(lst) c = copy.deepcopy(lst) #…
TheLazy
  • 253
  • 1
  • 15
3
votes
3 answers

Why Object.assign works with an array?

So I was reading an article to clone an object and array. All of them mentioned that Object.assign() can be used to copy an object but no one mentioned that Object.assign() will also work to shallow copy an array. Could anybody explain how it…
Samrat Saha
  • 585
  • 1
  • 7
  • 18
3
votes
1 answer

Shallow copy behavior in MATLAB

Lots of people bump into unexpected shallow copy behavior when they start off in Python, and I'd like to make sure I don't make any of those mistakes in MATLAB (with which I have less experience). I read this article about object behaviors in MATLAB…
3
votes
2 answers

I have two lists containing the same objects. How do I change one list without changing the other?

I first noticed this problem when I only put the objects in listOfRates and then created inverseListOfRates by copying it. But even using this method, I can't alter one list without altering the other. How do I solve this issue? List
user101
  • 175
  • 1
  • 10
3
votes
2 answers

Is it possible to make wrapper object for numbers, e.g. float, to make it mutable?

In Python 3 everything is supposed to be an object, even numbers, but they're immutable. Is it possible to create wrapper object for numbers, e.g. float, such that it would behave exactly as ordinary numbers except it must be mutable? I've wondered…
3
votes
1 answer

Does seq assignment create a new seq copy?

Given tow seqs, a and b, declared like this: var a = @[1, 2, 3] b = @[4, 5, 6] will a = b create a new seq copying everything from b to a or, reuse a? I have problems specially regarding to shallowCopy. I cannot tell what are they doing…
Arrrrrrr
  • 802
  • 6
  • 13
3
votes
1 answer

Having trouble in creating 2D array/list

I'm having difficulty creating a 2D list of permutations. Here is a minimal code to reproduce the problem class Solution: def permute(self, A): A = sorted(A) print A A_out = [] A_out.append(A) for iter0 in range(4): A[0]…
3
votes
5 answers

Copying objects to 'this' object in C#

I have a certain hirerchy of classes that needs the capeability to copy all public properties from one object to another. Each class has a certain set of public properties that might differ from any other class. Example: class Base { // Common…
the_drow
  • 18,571
  • 25
  • 126
  • 193