Copy & clone

Source: Internet
Author: User

When writing SQL center tests recently, we often need to write clone and copy tests. Due to this need, I have carefully reviewed clone (shallow copy) and copy (deep copy ).

We know:

Clone, shortest copy, the implementation is to copy the object value, one by one assigned to the object to copy. Note: assign values one by one. If it is a heap object, it is actually similar to the pointer copy in C language.

Copy, deep copy, the implementation of which is to create a copy of your own things, to the object to copy. In this way, they have the same wealth. There is no link between them except for equal quantities.

Below, we will try to upload images using livewriter using a self-help chart :)

Red indicates the original object, and blue indicates the cloned object. Suppose 1 represents the stack object, and OBJ represents the heap object.

/// B = A. Clone ();

The memory replication occurred during clone is a memory distribution chart. We can see that the heap object (A. OBJ and B. OBJ) points to the object at the same time.

Often, we need to test whether the object affects a and B when the object changes, that is, whether the clone is successful. Therefore

We have the following assertions:

// Change the object Value

Assert. areequal (A. OBJ, B. OBJ );

These two days, due to lack of a good understanding of this process, I often write suchCode:

B. OBJ = new objct ();

Assert. areequal (A. OBJ, B. OBJ );

Of course, it always fails. We can see that in the memory, B. OBJ and A. OBJ no longer point to the same memory.

But sometimes it is always wrong to think that a. OBJ and B. OBJ point to the same memory, changing B. OBJ and A. OBJ must also change at the same time. The mistake is ugly! :(

 

For deep replication:

Deep copy, which also assigns a value to the resource so that the object has different resources, but the content of the resource is the same. For heap resources, a heap memory is being opened to copy the original content. As in OBJ and newobj, they have the same content.

Of course, deep replication also has some replication depth problems. If there are heap resources in OBJ, whether to perform deep copy again is determined by the situation.

For a test of the class COPY method, the following test is required:

Assert. areequal (A, B );

Assert. areequal (A. OBJ, B. OBJ );

Assert. areequal (A. obj. Field, B. obj. Field)

.....

So far, I have learned about it. I used to think that I knew deep/shallow copy. After recent practices, I found that it is really difficult to use the theory in practice or to detect the theory in practice rather than deliberately!

In fact, the depth copy problem is reflected in many modes. If you have time, study it well.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.