This article focuses on value types
Memory partitioning model
In order to better understand the value type, first understand the memory partition model, in iOS, memory is mainly divided into stack area, heap area, global area, constant area, code area five areas. See the figure below
As is shown in
- The stack address is larger than the heap address.
- The stack is from the high address
->
Low address, extending down bysystem
Automatic management is one piececontinuous
Memory address. - The heap is from a low address
->
High address, extending upward fromThe programmer
Management, heap space structure is similar toThe list
, it isdiscontinuous
. - Overflow in everyday development refers to
Stack overflow
Can be understood as the stack and heap boundary collision. - The global area and constant area are stored in the __TEXT cString segment.
Value types
Characteristics of value types
- 1, the address stored in is
value
. - 2, in the process of passing the value type, equivalent to
pass
aA copy of the
So calledDeep copy
. - 3. State is not shared during value transfer
For example
func test() {
var age = 18
var age2 = age
age = 30
age2 = 45
print("age=\(age),age2=\(age2)")
}
test()
Copy the code
Breakpoint to view the stack address and memory of age
- Obtain the stack address for age:
po withUnsafePointer(to: &age){print($0)}
- Check the memory status of age:
x/8g 0x00007ffeefbff410
Also check the stack address and memory of age2
So you can see that when you assign age2, you’re just taking the value out of age and assigning it to age2. The age is 8 bytes away from the address of age2, indicating that the stack space is continuous and from the top to the bottom.
A structure is a value type
Define a structure and analyze it
struct PDTeacher {
var age : Int = 16
var age2: Int = 20
}
var t = PDTeacher()
print("end")
Copy the code
- Print t:
po t
We can see that t is printed as the value, without any information about the address
- Get the memory address of t and check its memory
If I assign t to T1, if I change T1, does t change?
- Print T and T1 directly, and you can find that T does not change because t1 changes, mainly because
t1
andt
Is betweenValue passed
That is, t1 and t are thetaDifferent memory space
, is to directlyt
The values in thecopy
tot1
In the. If you modify the value in T1, it is modifiedt1
The memory space is not affectedt
Of the memory space.
SIL verification
Similarly, we can verify that the structure is a value type by analyzing the SIL
- in
SIL
In the file, we look at the initialization method of the structure, and we can see that onlyinit
, and nomalloc
You don’t see anything about the allocation of the heap.
conclusion
A structure is a value type
And the address of the structure is the memory address of the first member.- Value types are directly in memory
Stored value
. - Value type assignment, is a
Value passed
The process is equivalent to a copy of a copy, stored in different memory space, two space between each otherUnshared state
. Value passed
In fact, isDeep copy
.