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 bysystemAutomatic management is one piececontinuousMemory address.
  • The heap is from a low address->High address, extending upward fromThe programmerManagement, heap space structure is similar toThe list, it isdiscontinuous.
  • Overflow in everyday development refers toStack overflowCan 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 isvalue.
  • 2, in the process of passing the value type, equivalent topassaA copy of theSo 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 tWe 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 becauset1andtIs betweenValue passedThat is, t1 and t are thetaDifferent memory space, is to directlytThe values in thecopytot1In the. If you modify the value in T1, it is modifiedt1The memory space is not affectedtOf the memory space.

SIL verification

Similarly, we can verify that the structure is a value type by analyzing the SIL

  • inSILIn the file, we look at the initialization method of the structure, and we can see that onlyinit, and nomallocYou don’t see anything about the allocation of the heap.

conclusion

  • A structure is a value typeAnd the address of the structure is the memory address of the first member.
  • Value types are directly in memoryStored value.
  • Value type assignment, is aValue passedThe process is equivalent to a copy of a copy, stored in different memory space, two space between each otherUnshared state.
  • Value passedIn fact, isDeep copy.