When does C++ perform deep copying and shallow copying?

shallow copy vs deep copy c++
shallow copy vs deep copy java
explain the difference between deep copy and shallow copy in sv
c# deep copy array
deep copy in c
deep copy c#
shallow copy and deep copy c#
shallow copy vs deep copy python

I wonder when C++ does deep copying, and when it does shallow copying.

For example:

int find()
{
    int n=5;
    return n;
}

In order to delete n after moving out of the function, it must create a temporary variable n, and return it back to the caller. As a result, that's shallow copy, is that right?

As a result, that's shallow copy, is that right?

A shallow copy keeps referring to whatever the original object† referred to. A deep copy refers to a copy of the referred resource†† (this new copy of the resource must be created by the copy constructor). That distinction is meaningful only if the copied object† is referential i.e. it refers to some resource.

int type does not refer to any object, so as far as the type system is concerned, it is not referential. However outside of the type system, one can give it referential meaning. It is quite typical for example, for an integer to represent identity of a resource such as an entity stored in a database. You need to consider whether that is the case for your integer. In object oriented design, such identifiers are typically wrapped into a class††† (which can be specified to support deep copies).

The copy of a non-class type is always shallow. Only copy constructor†††† can perform a deep copy. If 5 identifies some resource, then a copy of 5 also refers to that same resource.

Examples of referential types for which deep and shallow copy are relevant: References, pointers, classes which have referential members. Examples of usch classes: smart pointers, wrappers of referential identifiers†††. Of these, pointers and references are copied shallowly, since they aren't classes. Copy of a class instance may be shallow or deep, depending on the implementation of the copy constructor.

Then there is move construction. Move construction is a shallow copy, which modifies the original object in a way that enforces any class invariants that would be violated by a trivial shallow copy. For example, the move constructor of a unique pointer would shallowly copy the internal pointer, and set the original pointer to null, so that the uniqueness of pointer ownership is maintained.


† I say object for simplicity, but this applies to copies of a reference types - which are not objects - too.

†† A resource might be another object in memory, or for example a file descriptor, or a thread of execution.

††† Example: std::thread which wraps a lower level identifier provided by the OS.

†††† Any function can perform a deep copy, but a copy constructor is the only function that is invoked by copy initialisation.

copy in Python (Deep Copy and Shallow Copy), of dynamically allocated memory pointed to by the fields. OK. This is good for standard data types. A class can have one or more data members. How the copy occurs between the data members is what we are going to deal with this hub. When the Hub progresses, I will explain Shallow Copy, Deep Copy and the need for our own copy constructor. 2. ShalloC class. To demonstrate the need for the copy

The terms "shallow copy" and "deep copy" are generally understood to relate to types that themselves indirectly encapsulate some objects.

So, for example, a class with a pointer (that we assume points to something):

struct Foo
{
   Bar* ptr;
};

When you copy a Foo, is the Bar it points to copied as well (deep)? Or does the new Foo merely share the original pointer to the original Bar (shallow)?

This will depend upon how the copy is performed — usually your Foo will have a copy constructor, and it is the code in this copy constructor that makes the distinction.

For example, all standard C++ containers (e.g. vector) are internally made up of a bunch of pointers to some allocated buffers, but they have copy constructors that ensure the whole buffer is copied when the vector is copied, so that each vector has its own, independent buffer. This is a deep copy.

But the example I gave above, without any copy constructor or other lovely code, will simply perform a shallow copy when assigned, because I didn't tell it to do anything except to copy over the value of ptr.

As for your case: an int is just a value and so we can make no such comparison. You just have a value copy, plain and simple. The internal details of how many (if any) temporaries are required to implement that functionality are irrelevant (and unimportant); it's meaningless to talk about shallow vs deep copies in this case because there is literally no encapsulated, indirectly-held object owned or not-owned that can be deemed shallow or deep or anything copied. And we can generally say this about any non-class type.

C++: Shallow vs Deep Copies, Shallow copy works fine when dynamic memory allocation is not involved because when dynamic memory allocation is involved then both objects will points towards the same memory location in a heap, Therefore to remove this problem we wrote deep copy so both objects have their own copy of attributes in a memory. Deep copying. One answer to this problem is to do a deep copy on any non-null pointers being copied. A deep copy allocates memory for the copy and then copies the actual value, so that the copy lives in distinct memory from the source. This way, the copy and source are distinct and will not affect each other in any way.

int is not a referential type and so there is no question of shallow or deep copy. Its just a copy.

We can understand Deep and shallow copy as follows :

Deep Copy

A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.

Shallow Copy

Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the reference addresses are copied i.e., only the memory address is copied, not the actual objects.

Deep and Shallow Copy in JavaScript - Better Programming, This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the  Explanation of Deep and Shallow Copying. When creating copies of arrays or objects one can make a deep copy or a shallow copy. This explanation uses arrays. Recall array variables in Java are references (some folks say pointers, but there are differences between references and points). Object and array variables refer to the actual object or array.

Your description of the behaviour of your code is consistent with the source code that you present but the compiler might not do that.

C++ compilers are allowed to implement an as-if rule: i.e. your source specifies your intentions not the form of the final executable.

In other words, the compiler could replace your entire function call with 5.

In short, a deep copy creates duplicates every byte in your source object that's pertinent to that object. Any lesser copy than that is a shallow copy. Unless you overload = in some way, = takes a shallow copy.

Shallow copy and deep copy in C, When creating copies of arrays or objects one can make a deep copy or a shallow copy. This explanation uses arrays. Recall array variables in Java are  Its just a copy. We can understand Deep and shallow copy as follows : Deep Copy. A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers. Shallow Copy. Shallow copy is a bit-wise copy of an object.

Deep vs. Shallow copying., If the field is a value type, a bit by bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not, therefore the​  A shallow copy creates a new object which stores the reference of the original elements. So, a shallow copy doesn't create a copy of nested objects, instead it just copies the reference of nested objects. This means, a copy process does not recurse or create copies of nested objects itself.

Difference between Shallow copy and Deep copy, I was under the impression that this would run OK because the variables would all stay in scope until the end of the program and because  Difference between Shallow copy and Deep copy An object copy is a process where a data object has its attributes copied to another object of the same data type. In .Net Shallow copy and deep copy are used for copying data between objects.

9.15, Copying an object has always been an essential part in the coding paradigm. Be it in Swift, Objective-C, JAVA or any other language, we'll  In deep copy, all objects are duplicated, while in Shallow copy only top-level objects are duplicated and other lower level objects are referenced. For example, consider an object ' X ' that references objects ' A ' and ' B '. Object ' B ', in turn, references object ' C '. A shallow copy of ' X ' creates new object ' X2 ' that also references

Comments
  • For a single scalar value, what do you think the difference is between shallow and deep copying? How would it get any deeper? Also note, "delete" when it comes to function local, plain old data types is not a very useful concept; nothing is "deleted" typically, it's just left lying around as the stack is unwound, and the memory is probably reused in some later function call without being cleaned up in any meaningful way.
  • The "Shallow problem" happens with pointers; the address is copied but the content of the memory at that address is not copied. You have to manually copy it!
  • every copy is "shallow" unless you do something extra, in the sense that only the value is copied, but not anything that may be associated with it. Eg copying the pointer to the first element of an array does not copy the array. For your int there is absolutely no difference between shallow or deep copy
  • I've never heard of a language that does deep copies on its own.
  • @molbdnilo I really wanted to rock up and dazzle you with a counter-example, but I'm stumped!
  • Mostly true, although saying that a shallow copy is "a bit-wise copy of an object" is not really (at least not necessarily) correct in C++. You can still have high-level copy semantics without deep copying all your members. In fact unless you're doing a memcpy (or std::copy over an aliased char[]) that's pretty much always the case.