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

Do shallow copies share pointers? (C++)

I know that if I do something like this: class Obj { public: int* nine; }; Obj Obj1; //Awesome name int eight = 8; Obj1.nine = &eight; Obj Obj2 = Obj1; //Another Awesome name then Obj1's and Obj2's nines will point to the same 8, but will they…
user98188
0
votes
1 answer

Lazy copying - how to create a deep copy from shallow copy

I have a class which is using lazy copying - when a copy constructor is called, it creates shallow copy and when one method is called it creates a deep copy and add some more data. I'm stuck in part where I should create a deep copy from that…
0
votes
3 answers

How to Implement deep and shallow copy for NSMutableArray in iOS?

I am trying to implement deep and shallow copy for NSMutableArray, self.oldArray =[[NSMutableArray alloc] initWithCapacity:0]; self.shallowCopy =[[NSMutableArray alloc] initWithCapacity:0]; self.deepCopy =[[NSMutableArray alloc]…
Rooban Ponraj A
  • 281
  • 4
  • 20
0
votes
3 answers

How to implement two classes for automatic decision over deep and shallow copy?

I have the following design problem: I have a Resource with two sorts of accessors: one is to modify it (let's call it Access) one is for const-like access (let's call it Const_access), but you could say c1=c2 and then c1 will access c2. Given…
Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
-1
votes
0 answers

Avoid having to deep copy when trying to assign "const Node& inputnode" to member variable of type "Node*"

I'm relatively new to c++. I have a binaryTree class, that contains Nodes. Inside binaryTree, I want the "addleft" method to take in a node (and the potential subtree attached below it) and just "point" the root.left pointer to the "inputnode",…
JohnZ
  • 87
  • 8
-1
votes
0 answers

Which commands/methods return a deepcopy / shallowcopy / original?

"Python usually just assigns. Some commands produce a shallow copy (e.g., slicing, sorted(), dict .get(), ...). A deep copy is produced by copy.deepcopy, np.array and some other commands." How to improve that quote (of mine)? Mainly the second and…
Convexity
  • 99
  • 3
-1
votes
2 answers

I'm confused about how objects in shallow copy and deep copy are referenced

Shallow_Copy foo = [1, 2, []] bar = foo.copy() foo[0] is bar[0] foo[2] is bar[2] True True Does this mean they both reference to the same objects? However, they behave differently when I modify them. bar[0] = 0 print(foo) [1, 2, []]…
Aaron
  • 25
  • 4
-1
votes
1 answer

What is the efficient way to shallow clone an object using JavaScript?

//function for creating a shallow object and remove the multiple spaces export const getTrimmedValues = (values) => { const shallowValues = { ...values, }; for (const key in shallowValues.primaryInformation) { const currentValue =…
-1
votes
2 answers

Deep copy vs Shallow copy on iOS

What is the difference between deep copy and shallow copy on iOS world?? Please answer to this question very detailized!! e.g a piece of code class A { var name: String init(name: String) { self.name = name } } var a1 =…
-1
votes
1 answer

Shallow copy for arraylist

Hi i'm using the clone() method to copy the reference of my arraylist like this import java.util.*; public class Cloning { public static void main(String[] args) { ArrayList v = new ArrayList(); for(int i = 0; i < 10; i++ ) v.add(new…
-1
votes
2 answers

Javascript deep copy using JSON.parse and JSON.stringify

Is the below method a foolproof way of deep copying an object in Javascript i.e. even for very deeply nested objects/arrays ? let newObj = JSON.parse(JSON.stringify(obj));
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
-1
votes
1 answer

Why is the list.copy() method shallow?

In the official Python documentation said that list.copy() returns a shallow copy of the list. But according to the following code it is deep copy since the change of one list does not lead to the change in another. >>> num1 = [1,2,3,4,5] >>> num2 =…
-1
votes
1 answer

Understanding the shallow copy in Python

I have two sets of code which demonstrate shallow copy but I am not able to explain why the code behaves differently. The first set of code: import copy cv1 = [1,2,3] cv2 = copy.copy(cv1) print(cv1) print(cv2) cv2[0] = 0 cv1[1] =…
-1
votes
2 answers

Javascript spread notation and deep copy of array of object explanation

Consider the following code, let a =[{b:{c:100}}] let c = [...a] c[0].b = {l:200} console.log( a ) Output is: Array [Object { b: Object { l: 200 } }] Given c[0] is a new object and c[0].b is a reference. Changing the reference of b should…
muhammad800804
  • 105
  • 1
  • 8
-1
votes
1 answer

When is the global statement necessary?

I am a little confused about "global" statement. "sample code 1" runs ok without using "global" statement; however, "sample code 2" will not run unless the "global a" is un-commented. Why do I have to declare "global" in "sample code 2" but not in…