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

What is the fastest way to make a shallow copy of list in Python3.5+?

The are several alternatives to make a shallow copy of a list in Python 3.5+. The obvious ones are: some_list.copy() some_list[:] list(some_list) [*some_list] and others... Which method is the fastest? NOTE: While this question is related…
godaygo
  • 2,215
  • 2
  • 16
  • 33
12
votes
8 answers

How to use both default and custom copy constructor in C++?

I have a long class with a lot of data members. I want to write a copy constructor for it. But, if I write my own copy constructor, I lose access to the default copy constructor. I just want to repair a few pointers in my own copy constructor. So I…
pablo
  • 384
  • 2
  • 5
  • 17
11
votes
2 answers

Multiply operator applied to list(data structure)

I'm reading How to think like a computer scientist which is an introductory text for "Python Programming". I want to clarify the behaviour of multiply operator (*) when applied to lists. Consider the function make_matrix def make_matrix(rows,…
Aman Aggarwal
  • 3,905
  • 4
  • 26
  • 38
11
votes
5 answers

What is the difference between being shallowly and deeply equal? How is this applied to caching?

Found the following in my notes, but I am unable to make sense of it: Primitive type wrapper classes implement caching for a limited number of values. This guarantees that a limited number of deeply equal wrapper objects are also shallowly…
Albatross32
  • 279
  • 2
  • 4
  • 6
11
votes
1 answer

JS Recursive object assign

I learned that when using Object.assign() it extends only the top level object. How can I deeply extend the object? For example, let's say I have the following source object: const source = { id: 1, otherKey: {}, params: { page: { a:…
undefined
  • 6,366
  • 12
  • 46
  • 90
11
votes
2 answers

In python, is a function return a shallow or deep copy?

In python, if I have x = y any modification to x will also modify y, and I can do x = deepcopy(y) if I want to avoid modifying y while working on x Say, instead, that I have: myFunc(): return y def main(): x = myFunc() Is it still the…
user
  • 2,015
  • 6
  • 22
  • 39
11
votes
1 answer

How do I make a shallow copy of a Perl hash reference?

I want to push a reference to a hash. By that I mean I want to push a reference to a new hash that is a shallow copy of the hash I am given. How do I create the shallow copy?
Don
  • 4,583
  • 1
  • 26
  • 33
10
votes
1 answer

Shallow copy of mpz_t

GMP provides methods for initializing and assigning an mpz_t. A call to mpz_init_set(a, b) will assign to a the content of b. However, I assume, this performs a deep copy on b. On my project I need to work with arrays of mpz_t that are as long as…
James
  • 145
  • 3
  • 11
10
votes
2 answers

Scala case class uses shallow copy or deep copy?

case class Person(var firstname: String, lastname: String) val p1 = Person("amit", "shah") val p2 = p1.copy() p1.firstname = "raghu" p1 p2 p1 == p2 As i went through some documentation which says scala copy method of case class uses shallow…
Navin Gelot
  • 1,264
  • 3
  • 13
  • 32
10
votes
4 answers

Why are objects automatically passed by reference?

I have a general question about deep- and shallow-copy in the context of the pass-by-reference- and pass-by-value-concept of C#: In C# it is a requirement to explicitly create methods that accept pointers/references to be able to pass such to the…
Florian R. Klein
  • 1,375
  • 2
  • 15
  • 32
8
votes
1 answer

How to make a separate copy of an object in Perl 6?

I don't fully understand the docs, so I've tried clone, and it seems if there is an attribute of a mutable class, it can be changed in the new object using the old one (and that's what I don't want). How to make them (i.e. the copy and the original)…
Eugene Barsky
  • 5,780
  • 3
  • 17
  • 40
8
votes
3 answers

Java - Implement deep and shallow copy of an array

I am trying to understand the concept of shallow vs deep copy in Java. There is a lot of articles and Q&A about this subject, but whenever I try to implement these concepts in a real Java code, everything become unclear to me. One of the answers on…
Strider
  • 3,539
  • 5
  • 32
  • 60
8
votes
2 answers

In c# does Array.ToArray() perform a DEEP copy?

This should be a pretty basic question, but I've been having a little trouble finding a definite answer. When you have an array of values and you use the .ToArray() method does it create a deep or shallow copy of the array?
hrh
  • 658
  • 1
  • 14
  • 24
7
votes
4 answers

C++ compiler 'shallow' copies and assignments

I'm taking a class on object oriented programming using C++. In our text it says, If we do not declare a copy constructor, the compiler inserts code that implements a shallow copy. If we do not declare an assignment operator, the compiler inserts…
bigcodeszzer
  • 916
  • 1
  • 8
  • 27
7
votes
2 answers

Shallow copy reference into variable in Perl

In Perl, you can assign to a variable a reference to another variable, like this: my @array = (1..10); my $ref = \@array; And, as it is a reference, you can do something like this and both variables will be affected: push @array, 11; push @$ref,…
Francisco Zarabozo
  • 3,676
  • 2
  • 28
  • 54
1 2
3
26 27