The compiler automatically generates initializers for structs that can pass in values

The definition of a class is similar to that of a structure, but the compiler does not automatically generate initializers for classes that can pass in member values

The essential difference between a structure and a class A structure is a value type (enumeration is also a value type) and a class is a reference type (pointer type)

Size is the memory address of the pointer variable size object

A location in the stack space that stores the memory address of an object, and the address refers to a location in the heap space that allocates the memory address

Memory address analysis, you get the call that STRCT does not generate the corresponding alloc malloc instantiation method

  • callq 0x100002ae0 ; Test.testClassAndStruct() -> () at main.swift:2
  • Test`init() in Point #1 in testClassAndStruct():
  • -> 0x100002b80 <+0>: pushq %rbp
  • 0x100002b81 <+1>:  movq   %rsp, %rbp
    Copy the code
  • 0x100002b84 <+4>:  xorps  %xmm0, %xmm0
    Copy the code
  • 0x100002b87 <+7>:  movaps %xmm0, -0x10(%rbp)
    Copy the code
  • 0x100002b8b <+11>: movq   $0x3, -0x10(%rbp)
    Copy the code
  • 0x100002b93 <+19>: movq   $0x4, -0x8(%rbp)
    Copy the code
  • 0x100002b9b <+27>: movl   $0x3, %eax
    Copy the code
  • 0x100002ba0 <+32>: movl   $0x4, %edx
    Copy the code
  • 0x100002ba5 <+37>: popq   %rbp
    Copy the code
  • 0x100002ba6 <+38>: retq   
    Copy the code

class Size { var x = 1 var y = 2 }

var size = Size()

0x100002b42 <+34>: callq 0x100002b80

; Size.__allocating_init() -> Size in Test.testClassAndStruct() -> () at main.swift:7`

Enter the slowAlloc content malloc content to make the call

The proof is to apply memory to the heap space

Size Specifies the Size of memory that the pointer variable points to, which is an 8/ byte memory address used to point to the heap space

Value types

The value type is assigned to the var let or passed to the function to copy a copy of all internal files, which is similar to copying a file. The new file copy is a deep copy

In the Swift standard library, the String Set uses the copy on write method to improve performance. The memory address is changed only when the modification is started. The copy operation is actually performed only when the write operation is performed

Assignment operations of value types can overwrite the corresponding memory information,

An assignment operation of a reference type

Value type, reference type of let

That is, the value type cannot be modified, the memory of the value type cannot be modified, struct memory stores all the value content and therefore all other contents cannot be modified

Reference types

The memory of S class can’t be modified, the contents of the memory of class can’t be modified but the properties of class can be modified

How many memory requests does a class_getclassInstance object have

Do methods take up memory for objects? Where are methods stored that do not occupy the object’s memory? Methods, functions are stored in code snippets

The printed address value is probably stored in heap space if the middle 1 is not followed by four zeros

Closure Expression {//in = Closure Expression; //in = Closure Expression; //in = Closure Expression;

Closure: A function combined with its captured constant/variable environment is called a closure

~ usually refers to the external function that is captured. ~ Usually captures the variables – constants of the external function

You can think of a closure as an instance object of a class, memory is stored in heap space, and since the local variables/constants captured by methods that call alloc are the members of the object and the functions of the closure are the methods defined inside the class different closure instance objects have their variables captured, Both stack space and heap space have a num, but since PLUS captures num, it stores the value of a heap space

“typealias Fn = (Int) -> Int func getFn() -> Fn { var num = 0 func plus(_ i:Int) -> Int{ num += i return num } return plus }

var fn = getFn()“

/** 1:getFn stores the function in the corresponding data segment closure. Alloc is called mainly to store num value and apply for the corresponding heap space, so the corresponding memory space is called once