Questions tagged [deep-copy]

A deep copy of an object is a separate, fully independent duplicate of that object, such that any references / pointers it holds to other objects refer to deep copies of those to which the original's refer. Use this tag for questions regarding implementing or using deep copying methods.

Deep copying is a process by which an object is fully duplicated to create a copy that is wholly independent of the original. This differs from shallow copying with respect to the treatment of references / pointers to other objects: deep copying makes a deep copy of each referenced object, too, whereas shallow copying copies only the references. Deep copying is "deep" in the sense that it traverses the object's reference graph all the way to the bottom.

Related tags

Links

1483 questions
33
votes
2 answers

Relationship between pickle and deepcopy

What exactly is the relationship between pickle and copy.deepcopy? What mechanisms do they share, and how? It is clear the two are closely-related operations, and share some of the mechanisms/protocols, but I can't wrap my head around the…
shx2
  • 61,779
  • 13
  • 130
  • 153
33
votes
3 answers

Deep Copy of OpenCV cv::Mat

The behaviour of copying cv::Mat is confusing me. I understand from the documentation that Mat::copyTo() is deep copy while the assignment operator is not. My questions: what should I do to return a cv::Mat from a function, such as: cv::Mat…
flyinggip
  • 341
  • 1
  • 3
  • 4
33
votes
5 answers

copy.deepcopy vs pickle

I have a tree structure of widgets e.g. collection contains models and model contains widgets. I want to copy whole collection, copy.deepcopy is faster in comparison to 'pickle and de-pickle'ing the object but cPickle as being written in C is much…
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
32
votes
3 answers

javascript deep copy using JSON

I have problem with javascript object(array) deep copy. I read many good way to deal with it. And I also know that jQuery has $.extend API to this problem. But my question is: Can I just using JSON stringify and parse method to solve this…
user2666750
  • 582
  • 1
  • 7
  • 11
31
votes
4 answers

How to do "Deep Copy" in Swift?

In Objective-C, one can deep-copy by following: Foo *foo = [[Foo alloc] init]; Foo *foo2 = foo.copy; How to do this deep-copy in Swift?
allenlinli
  • 2,066
  • 3
  • 27
  • 49
30
votes
6 answers

Deep copy duplicate() of Java's ByteBuffer

java.nio.ByteBuffer#duplicate() returns a new byte buffer that shares the old buffer's content. Changes to the old buffer's content will be visible in the new buffer, and vice versa. What if I want a deep copy of the byte buffer?
Mr. Red
  • 301
  • 1
  • 3
  • 3
29
votes
5 answers

Deep copy Map in Groovy

How can I deep copy a map of maps in Groovy? The map keys are Strings or Ints. The values are Strings, Primitive Objects or other maps, in a recursive way.
Ayman
  • 11,265
  • 16
  • 66
  • 92
28
votes
3 answers

Concisely deep copy a slice?

In Go, what's a concise/well-performing way to deep copy a slice? I need to copy the slice to a new backing array, because the other array is owned by something else and may be modified after the copy. I'm currently doing it like this: copy :=…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
27
votes
2 answers

Does a slicing operation give me a deep or shallow copy?

The official Python docs say that using the slicing operator and assigning in Python makes a shallow copy of the sliced list. But when I write code for example: o = [1, 2, 4, 5] p = o[:] And when I write: id(o) id(p) I get different id's and also…
user2528042
  • 419
  • 2
  • 6
  • 12
27
votes
4 answers

What's the best way to deep copy a hash of hashes in Perl?

Possible Duplicate: What's the best way to make a deep copy of a data structure in Perl? Before I start coding this myself and reinventing the wheel, how do you copy a hash of hashes without duplicating the hashrefs? I'm reading a hash of hash of…
Oesor
  • 6,632
  • 2
  • 29
  • 56
27
votes
3 answers

C++: Deep copying a Base class pointer

I searched around and seems in order to perform this I need to change my Base class and want to know if this is the best approach. For example, I have a Base class: class Base {} Then a long line of derived classes: class Derived_1:: public Base…
madu
  • 5,232
  • 14
  • 56
  • 96
26
votes
7 answers

clearing or set null to objects in java

I was recently looking into freeing up memory occupied by Java objects. While doing that I got confused about how objects are copied (shallow/deep) in Java and how to avoid accidently clearing/nullifying objects while they are still in use. Consider…
as.tek
  • 937
  • 3
  • 14
  • 20
26
votes
3 answers

R, deep vs. shallow copies, pass by reference

I would like to understand the logic R uses when passing arguments to functions, creating copies of variables, etc. with respect to the memory usage. When does it actually create a copy of the variable vs. just passing a reference to that variable?…
Alex
  • 19,533
  • 37
  • 126
  • 195
25
votes
5 answers

Pulling data from a CMSampleBuffer in order to create a deep copy

I am trying to create a copy of a CMSampleBuffer as returned by captureOutput in a AVCaptureVideoDataOutputSampleBufferDelegate. Since the CMSampleBuffers come from a preallocated pool of (15) buffers, if I attach a reference to them they cannot be…
bennyty
  • 371
  • 5
  • 18
25
votes
10 answers

Copying an array of objects into another array in javascript (Deep Copy)

Copying an array of objects into another array in javascript using slice(0) and concat() doesnt work. I have tried the following to test if i get the expected behaviour of deep copy using this. But the original array is also getting modified after i…
jsbisht
  • 9,079
  • 7
  • 50
  • 55
1 2
3
98 99