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

Javascript Shallow copy of an object is undefined?

I just started learning Javascript about a week ago so bear with me. I am attempting to make a basic physics engine using a quadtree and nodes as the backing structure for all objects. However in this snippet of code 102 Quadtree.prototype.insert=…
jknam
  • 93
  • 7
3
votes
1 answer

Networkx copy clarification

According the doc, it appears that the networkx.copy method does a deep copy of the graph. I'm most concerned about the statement This makes a complete copy of the graph including all of the node or edge attributes. Is this suggesting that it…
sedavidw
  • 11,116
  • 13
  • 61
  • 95
3
votes
5 answers

Java: Copy attributes from one object instance to another?

Say you have public class Car{ private Engine m_eng; public Car(){ } public Engine getEngine(){ return m_eng; } public void setEngine(Engine engine){ m_eng = engine; } } public class Engine{ private String m_name; …
u19964
  • 3,255
  • 4
  • 21
  • 28
3
votes
5 answers

Deep Copy and Shallow Copy Java

I am studying for a java exam and what I found out practically differed from what is taught in theory. Below is code: StringBuilder num3[]= new StringBuilder[2]; num3[0]= new StringBuilder("Pine"); num3[1]= new StringBuilder("Oak"); …
3
votes
2 answers

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Error

This error occurs during run time, and I'm not sure what's causing it - the code looks correct to me. #include #include using namespace std; struct Room { int d_noSeat; bool d_hasProjector; Room() = default; …
Will
  • 59
  • 1
  • 1
  • 6
3
votes
1 answer

Does the default Assignment operator create memory leak, when shallow-copying pointers?

I'm new to this website, and to the programming world. So, please be patient with me :) I read about the rule of three, and I understood how the Copy Constructor and the Assignment operator work. So I understood that, the default ones, provided by…
3
votes
4 answers

Shallow/deep copy of std::map

How would I best implement these? I thought of something like this: using namespace std; shape_container shape_container::clone_deep () const { shape_container* ptr = new shape_container(); copy( data.begin(),…
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
3
votes
3 answers

how to create deep copies of structures

How do I copy a structure in Common Lisp? I created a structure like: (defstruct state board player previous-move depth) Board is a 2 dimension array. I tried doing: (setf new-state state) When I change something in new-state, the changes in…
FPTLS
  • 177
  • 3
  • 15
3
votes
1 answer

Enum type reference or primitive (with example) - shallow/deep copy

My question is very basic, but I would like to understand everything in 100%. Many questions in SO referes to my post, but I haven't find a satisfying answer. We know that Enums in java are reference type. Let's consider the following…
radekEm
  • 4,617
  • 6
  • 32
  • 45
3
votes
6 answers

Why does copy of the List still change properties in the original List using C#

Lets say I have this class public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public bool isActive { get; set; } } And use it like this: List Employees = new…
user850010
  • 6,311
  • 12
  • 39
  • 60
3
votes
3 answers

Java: Vector add function is it shallow?

When you use the add function to add an object to a vector, is it a shallow copy or deep copy? If it's shallow it means if you change the objects in the vector you would change the original copy of the object?
roverred
  • 1,841
  • 5
  • 29
  • 46
3
votes
1 answer

Does stl library use malloc while copying pointers?

I have a question regarding copying pointers in the stl library. Say I define: struct A{ int x; } std::map map1; I then populate map1 using memory from the heap using malloc for the pointer to A. Then I do std::map
3
votes
1 answer

Construct Delegate from Delegate. What does the new Delegate Point to?

Consider the following: Action a1 = new Action(_insert); Action a2 = new Action(a1); What is a2 referring to ? Is it a1, a shallow copy of a1 or is it a deep copy of a1 ?
marc wellman
  • 5,808
  • 5
  • 32
  • 59
2
votes
2 answers

Copy constructor c++ weird behavoir?

Hi I have a class which includes an array, I'm not passing this through my class constuctor (could be going wrong here?) The array is just defined in the class then initialized in a read method. But in the main I make an instance of the class…
rx432
  • 93
  • 1
  • 3
  • 10
2
votes
2 answers

python deepcopy and shallow copy and pass reference

A question about python deepcopy and shallow copy. the post at What is the difference between a deep copy and a shallow copy? cannot help me. why e.g. 1 's sum is 6 not 10 ? e.g.1 : kvps = { '1' : 1, '2' : 2 } theCopy = kvps.copy() # both point…
user1002288
  • 4,860
  • 10
  • 50
  • 78