Shows a shallow copy: for non-basic data types, after clone, the result is two pointers pointing to the same memory space, so it is only a shallow copy, so if the operation on one object, the other content will also change, it is obviously unreasonable, should each object to save their own data.So we're going to make a deep copy!Shallow
.1.3 Reference count increase case(1). Object created: x=4(2). Another person was created: y=x(3). Passed as a parameter to the function: Foo (x)(4). As an element of a container object: a=[1,x, ' 33 ']1.4 Reference count reduction(1). A local reference leaves its scope. For example, at the end of the foo (x) function above, X points to the object reference minus 1.(2). The alias of the object is explicitly destroyed: Del x; or del y(3). An alias of an object is assigned to another object: x=7
Before you can understand deep and shallow copies, you need to understand some basic concepts, and the types of variables stored in memory are classified as value types and reference types.
1, the value type of the storage characteristics of the variable, the data in all copies of a copy, stored to the new variable.
For example: var num = 123 var num1=num;
Indicates that the number stored in the variable is 123. Then
The following small series brings us a brief discussion of object-oriented deep copy and shallow copy in JavaScript. Small make up feel very good, now share to everyone, also give you a reference.
1. Shallow copy: Copy a reference, all reference objects point to a piece of data, and can modify this data.
2. Deep
ObjectiveAchieve the optimization purpose of the deferred copy (lazy copy). Similar to deferred initialization (lazy initialization), the choice is more effective at the right time.Alias
COW (Copy-on-write)
Lazy Copy
MotivationCopying objects can sometimes result in a performance penalty (performance
Copy Implementation method
Copy
will only produce immutable replica objects (such as NSString)
Mutablecopy
Only a mutable copy object (such as nsmutablestring) is generated
To use copy, you must follow the protocol and implement the Copywithzone method
-(ID) Copywithzone: (Nszon
One, why use copy new when new objects are created, is going to cost a great deal. In order to solve this problem java provides Span style= "font-family: ' Consolas ';" >cloneable this excuse. The class that implements this excuse has the ability to be copied, and the copy is done in memory, rather than through the We know that a copy has a depth, w
Before understanding the depth of JS copy, I think it is necessary to mention about value passing and reference passing.In JS, the copy of the base type value is passed by value, while the copy of the reference type value is passed by reference. Value-passing copied objects are not implicated, independent of each other, but the objects that refer to replication a
Copy constructor, not byte-to-byte copy, but member-to-member copyconstructor, a parameter is a reference to a classThat's right1#include 2#include string>3 using namespacestd;4 5 Static intObjectcount =0;6 7 classHowmany8 {9 Public:TenHowmany () {objectcount++; print ("Howmany ()"); }//constructor, no arguments OneHowmany (inti) {objectcount++; print ("howmany (int)"); }//constructor, the argument is an i
One, shallow copy, deep copyA shallow copy copies a member variable in an object: if it is a basic type, the copy is the value of the base type, and if the property is a memory address (reference type), the memory address is copied;Deep Copy, in addition to the basic type, the object referenced by the reference type is
Control object Copy, assignment, destructorCopy constructor, move constructorCopy assignment operator, move assignment operatorDestructors--------------------------------------------------------------------------------------------------------------- ----------------------1. Copy constructor: The parameter must be a reference type , typically a constThe 1th parameter of a
1. For basic data TypesIts value occupies a fixed amount of space in memory and is stored in the stack memory. When a variable copies the value of a base type to another variable, a copy of the value is created, and we cannot add attributes to the value of the base data type. It is a deep copy.2. For reference typesShallow copy:
Recently, colleagues have encountered the issue of the depth of the copy and assignment, today I also study the record, deepen the memory.To give a simple example:var people = {Age:10,Name: "Xiao Hua",arr:[1,2,3]};To do an assignment operation:var people2 = people;Then do a shallow copy operation:var people3 = {};for (var i in people) {People3[i] = People[i]};And then finally do a deep
in Java incorporate the object class by default, and a clone () method is provided in the object class. The purpose of this method is to return a copy of an object. This copy function returns a new object instead of an application. So how do you use this method? The following are steps for using the Clone () method.
1, the implementation of the Clone () class, the first need to integrate the Cloneable inte
In this article we will learn about the Python dictionary
python copy functionThe relevant knowledge,
python copyWhat it means is that what he does will be answered in the next article.
Summary description
The Python dictionary (Dictionary) copy () function returns a shallow copy of a dictionary.
Grammar
Copy () metho
Original blog: http://blog.csdn.net/qq_29329775/article/details/49516247There has been a recent error in doing algorithm jobs because there is no clear distinction between deep replication and shallow replication of Java collections.1. First, the understanding of shallow copy and deep copy of the Java collection objectA normal collection copy simply copies the ad
---restore content starts--- Span style= "Background-color: #3366ff" > numbers and strings , N Bsp , NB Sp , NB Sp , NB Sp , NB Sp Both the numbers and the memory in the string point to the same address, so deep copies and shallow copies are meaningless to them.Import Copya = 123 #赋值print (ID (a)) #输出存储变量的地址b = Aprint (ID (b)) B = Copy.copy (a) #浅拷贝print (ID
"138-copy list with random Pointer (copy of single-linked list with random pointers)""leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"Original QuestionA linked list is given such this each node contains an additional random pointer which could point to all node in the list or null.Return a deep copy of the list.Main TopicA si
Take the string class as an example:650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/3E/wKioL1bjlczjmUcPAAA0DnKyOK0378.png "title=" 1.PNG " alt= "Wkiol1bjlczjmucpaaa0dnkyok0378.png"/>A bit copy is a copy of the address, and the value copy copies the contents.If you define a string class, two objects are str1,str2. Str1._str and Str2._str point to a s
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.