answersLogoWhite

0

Shallow copies occur whenever we copy "naked" pointers.

int* p1 = new int {42};

int* p2 = p1; // shallow copy

We call this a shallow copy because although p1 and p2 are independent pointer variables, they both refer to the same memory. If we change the value stored in that memory, we affect both pointers:

assert (*p2 == 42);

*p1 = 0;

assert (*p2 == 42); // error!

Side-effects such as this are completely at odds with how a "normal" copy behaves:

int x = 42;

int y = x;

assert (y == 42);

x = 0;

assert (y == 42); // ok

Here, changing the value of one variable has no effect upon the other because they are completely independent variables.

To mimic this behaviour with pointer variables we must perform a deep copy, copying the memory being referred to:

int* p1 = new int {42}; int* p2 = new int {*p1}; // deep copy

assert (*p2 == 42);

*p1 = 0;

assert (*p2 == 42); // ok

A shallow copy is (usually) cheaper than a deep copy, thus it is not unusual for class designers to postpone an expensive deep copy until it becomes absolutely necessary to do so (also known as a lazy copy). However, since C++11, the inclusion of move semantics and standard library smart pointers and resource handles has greatly reduced the need for shallow copying.

User Avatar

Wiki User

7y ago

What else can I help you with?

Related Questions

What is c plus c plus c plus c plus c?

c + c + c + c + c = 5 * c.


How do you concatinate two files in c plus plus?

Copy the first file then append the second file to the copy.


What is difference between Deep copy and shallow copy in iphone?

Shallow copy is duplicate little as possible. because of it copy one object to another object. it copy only structure not elements . Deep copy means Duplicate everything. it will be copy structure as well as elements; e.g :- char A*={'a','b','c'}; char B*={'x','y','z'}; B=A;


What are the two ways of initializing a structure variable in c plus plus?

Two ways? There are at least four: 1) Zero the memory allocated to the structure. 2) Shallow-copy the memory from another structure of the same type. 3) Deep-copy the memory from another structure of the same type. 4) Set the individual members.


C program plus copy of string to another?

strcpy


Users can use a shortcut such as CTRL plus C for operations they perform frequently?

CTRL C to copy


What command is the control key plus c?

Copy the selected text (or object).


What is the difference in shallow copy and deep copy in mobile application development process?

Shallow copy also known as address copy involves copying of the address and not the actual data. In case of deep copy, the data is copied.


What is the use of print option in c plus plus?

C++ has no print option. The print option in your IDE allows you to print your C++ source code, thus giving you a "hard" copy of your code.


How is a bitwise copy created in c plus plus?

You could just use memcpy(3), using sizeof() to get the object size.


What actors and actresses appeared in Shallow Copy - 2009?

The cast of Shallow Copy - 2009 includes: Jolee Kim as Tabitha Demetrius Sager as Will Michael Thomas Moore as Max


What is a sample code for C plus plus?

Copy and paste this and it will say hello world! #include <iostream> int main() { std: :cout < < "Hello, world!\n ";