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
5
votes
2 answers

when would I use a shallow copy?

I understand what the difference between a shallow and deep copy are, but I really don't understand in what situations a shallow copy would be preferred. If I'm not mistaken, shallow copy makes a new copy of the value types and simply copies the…
David Torrey
  • 1,335
  • 3
  • 20
  • 43
5
votes
2 answers

Is shallow copy sufficient for structures with char[]?

I have a structure containing character arrays with no any other member functions. I am doing assignment operation between two instances of these structures. If I'm not mistaken, it is doing shallow copy. Is shallow copy safe in this case? I've…
jasonline
  • 8,646
  • 19
  • 59
  • 80
5
votes
2 answers

Deep Copy Constructor for binary tree

I am trying to create a deep copy of my binary tree data structure in C++. The problem is the code I am using only seems to be giving me a shallow copy (which seems to cause problems with my deconstructor). the code below is my binary tree copy…
brian4342
  • 1,265
  • 8
  • 33
  • 69
4
votes
2 answers

Does Spread Syntax create a shallow copy or a deep copy?

I am extremely confused for days now regarding the true definition of a shallow copy and a deep copy. When I read the mdn docs (https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) on shallow copy, it all made sense. The first paragraph…
espc
  • 57
  • 8
4
votes
1 answer

Is vector::push_back() making a shallow copy & how to solve this

In the program I am writing, I have something similar to the code here: #include #include #include using namespace std; struct people { string name; int st; int sn[50]; }; int main() { unsigned int…
zw324
  • 26,764
  • 16
  • 85
  • 118
4
votes
1 answer

Java: Why String is special in Deep Copy and Shallow copy?

These days I am learning the clone method of Java. I learned that clone uses shallow copy. If I want to implement a deep copy of an object, then I should follow the following principles from this website No need to separately copy primitives. All…
chenlangping
  • 101
  • 2
  • 7
4
votes
3 answers

shallow copy in python

I am a little confused on how shallow copy works, my understanding is when we do new_obj = copy.copy(mutable_obj) a new object is created with elements of it still pointing to the old object. Example of where I am confused - ## assignment i = [1, 2,…
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
4
votes
2 answers

Does module.exports in node js create a shallow copy or deep copy of the exported objects or functions?

For example, If I have 2 modules try1.js and try2.js try1.js module.exports.msg = "hello world"; try2.js try1 = require('./try1'); try1.msg = "changed message"; Does the change in the contents of msg made in try2.js affect try value of…
4
votes
4 answers

System.arraycopy() shallow copy or deepcopy with primitive and object references

I read somewhere that System.arraycopy does create a new copy for primitive data types and shallow copy for object references. so, that I started the experiment that with below code //trying with primitive values int a[] ={1,2,3}; int b[] = new…
gajapathy p
  • 113
  • 1
  • 13
4
votes
3 answers

What is the difference between MemberwiseClone() and Assigning a Reference type in C#?

If i have a code like this class Student { public string RollID { get; set; } } class Person { public Student student { get; set; } public string Address { get; set; } public string Name { get; set; } public Person Clone() …
Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
4
votes
3 answers

How can I create a deep copy of my list collection

Suppose I have the following class: public class Author { public int ID {get; private set;} public string firstName {get; private set;} public string lastName {get; private set; } public Author(int id, string firstname, string…
user6029770
4
votes
1 answer

Angular.copy() not deep copying referenced arrays

In my Angular application, I have an array that refers to the coordinates of a polygon. Eg: [[-1,0], [0,1], [1,0], [0,-1], [-1,0]] The important bit here is that the that the first and last points are repeated, and actually reference the same…
Will Durney
  • 1,168
  • 2
  • 13
  • 16
4
votes
2 answers

How do I create a shallow copy of an IEnumerable?

I have an IEnumerable object as: IEnumerable listSelectedItems; Which contains three items. Now i created a new object and want to get all items from listSelectedItems, so i wrote this code: IEnumerable newList =…
Murtaza Munshi
  • 1,065
  • 4
  • 13
  • 40
4
votes
1 answer

How to create an instance of a value type "by reference"

Consider the code and output: using Microsoft.Xna.Framework; //Where color is from ^ that static Color color = new Color(0, 0, 0, 0); static void Main(string[] args) { Color otherColor = color; color.B = 100; …
Colton
  • 1,297
  • 14
  • 27
4
votes
2 answers

Shallow copy: why is list changing but not a string?

I understand that when you do a shallow copy of a dictionary, you actually make a copy of the references. So if I do this: x={'key':['a','b','c']} y=x.copy() So the reference of the list ['a','b','c'] is copied into y. Whenever I change the list (…
jujae
  • 51
  • 4