Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is deep replication and shallow replication in C++?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "what is deep copy and shallow copy in C++". The content is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn this article "what is deep copy and shallow copy in C++".

1. What is shallow replication

The biggest difference between deep copy and shallow copy in C++ is when the class contains pointer-type data members. Because the default copy constructor completes the numerical copy of the object members, when the original object contains a pointer P pointing to the address xxx, the pointer P of the new object copied through the original object also points to the address xxx, causing the same address xxx to be pointed to by two objects at the same time, which is very insecure, because the operation of either object on the address xxx is likely to adversely affect the other object. The Duck class defined below contains the pointer member foot, which points to the first address of the array:

Class Duck {public: Duck () {foot = new int [2] ();} int * foot;}

First define a duck object, and then copy the anotherDuck through duck, so the pointer member foot of both points to the same address. AnotherDuck operates on the memory pointed to by the pointer member foot, which is equivalent to the pointer member foot of duck, because the foot pointers of both objects point to the same address, which is the source of insecurity:

Duck duck;Duck anotherDuck = duck;/// anotherDuck assigns foot to memory anotherDuck.foot [1] = 666

In addition, to prove that the foot pointers to both the original object and the new object point to the same address, take a look at:

Printf ("their id is% p and% p\ n", duck.foot, anotherDuck.foot); / the results show that they do point to the same address their id is 005B0CE8 and 005B0CE8

In short, the above phenomenon is shallow replication, which is likely to lead to insecurity, which is also reflected in the release of memory (the same memory cannot be freed twice), so you need to use the deep replication described below.

2. How to achieve deep replication

According to the information available, deep copying requires writing an assignment constructor to create a new copy of the memory pointed to by the member pointer. For example, the Duck class pointer member foot defined in the previous section, the copy constructor needs to create a new copy of the memory pointed to by foot:

Duck (Duck & duck) {/ 1, create a new memory space foot = new int [2] (); / / 2, copy all the array values pointed to by the pointer of the original object to the array for pointed to by the pointer of the new object (int I = 0; I < 2; iObject +) {foot [I] = duck.foot [I];}}

The purpose of the first step is to create a new memory space so that the pointer members of the new object point to the new memory instead of pointing to the same memory as the original object, and must ensure that the new memory stores the same type as the original object, which is an array of two elements of type int. The purpose of step 2 is to copy all the values of the array pointed to by the original object pointer to the array pointed to by the new object pointer. After the two steps above, the deep copy is completed.

To ensure that the foot pointers to the original object duck and the new object anotherDuck point to different addresses, you can do the following test to output the addresses that the two foot points to:

Printf ("their id is% p and% p\ n", duck.foot, anotherDuck.foot); / / the results show that the two foot points to different addresses their id is 01250FA0 and 01250B40 above is all the contents of this article "what is deep copy and shallow copy in C++". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report