1. Deep copy and shallow copy
First of all, we need to understand that js data types are divided into:
Basic data types (Number, String, Boolean, Null, Undefined, Symbol)Values that refer to data types are objects that are held in stack memory and heap memory. Stack memory holds the variable identifier and a pointer to the object in heap memory. When looking for a reference value, the interpreter first looks for an address in the stack. Then locate the entity in the heap memory according to the address.
2. Why do you want to copy in depth
First look at the following codeCopy the code
let a = b = 2
a = 3
console.log(a)
console.log(b)
let c = d = [1.2.3]
let e = f = {a:1.b:2.c:3}
c[0] = 2
e.a = 2
console.log(d[0])
console.log(f.a)
Copy the code
When you assign the same Array or Object to two different variables that point to the same memory address, one variable changes its property value and the other changes its corresponding property value.
In most real projects, we want two variables (with the same initial value) to be independent of each other. So copy is used (there are two shades)
Three, the difference between the depth of the copy:
Shallow copy of the original object is the data type field copy into a new object, type will refer to the “reference” field is copied to the new object, don’t copy “reference objects”, so the original object and the new object references to the same object, a new object references in the field changes can lead to the corresponding fields in the original object is changing. A deep copy is to create a new field with the same content as the original field, which is two data segments of the same size. Therefore, the references of the two fields are different. Changes to the referenced fields in the new object do not change the fields in the original object.Results:
A structure
Two destructorWhen we do not have our own copy constructor, we will call the default copy constructor when we copy the object and make a shallow copy. That is, after we copy the pointer name, there will be two Pointers to the same memory block. So the destructor is destroyed twice, resulting in a memory leak.Therefore, when copying “objects containing pointer members”, you must define the copy constructor to ensure that the pointer members of the copied objects have their own memory space, that is, deep copy is performed to avoid memory leaks. test.cpp Results:
To construct a
Copy construct once
Destructor twiceTo sum up, a shallow copy only copies the pointer, and the two Pointers point to the same block of memory. A deep copy copies both the pointer and the contents that the pointer points to, and the copied pointer points to a different pointer.
Deep copy and shallow copy are for complex data types. The shallow copy copies only one layer, while the deep copy copies multiple layers.
Deep copy
Deep copy copies the value of a variable. For a variable of a non-basic type, it recurses to a variable of a basic type before copying. Objects after a deep copy are completely isolated from the original ones. Modifications to one object do not affect the other. Shallow copy
A shallow copy copies each property of an object in turn, but when the property value of an object is of a reference type, the actual copy is the reference and changes as the reference changes. You can use the for in, Object. Assign, extension operator… Array.prototype.slice(), array.prototype.concat (), and recursion implement deep copy