Lattice 16: The Block I understand (3)

Source: Internet
Author: User

23. In the example above, the ISA pointer in the block structure has not been explained in detail, and this pointer has been set to _nsconcretestackblock, which identifies the type of block.

In fact, in addition to _nsconcretestackblock this type, block there are other types, these types have a total of 3 kinds:

(1), _nsconcretestackblock

(2), _nsconcretemallocblock

(3), _nsconcreteglobalblock

24. These 3 types represent the area where the block object is stored, such as:

25, the compiler mode will also affect the storage of the Block object, the same block object in Arc mode and in MRC mode may be stored in different areas. Next, use the code to examine how the global block, the normal block, the block of the intercepted variable, and the block that intercepts the mutable variable are stored in ARC mode and in MRC mode.

26, you can print out the Block object to determine where it is stored, the 3 types of block objects printed out classes are:

(1), __nsstackblock__

(2), __nsmallocblock__

(3), __nsglobalblock__

General Global Block

27, first in the ARC mode, write a general block, and print out the variables that hold the Block object, and the copy value of this variable:

It can be found that the block objects printed in both cases are __NSGLOBALBLOCK__ classes, indicating that they are stored in the. Data area.

28. Try executing the same code in MRC mode:

Found the same effect. Indicates that the normal global Block is the same as the storage area in the Arc mode and in the MRC mode, which is the. Data area.

Intercept the global block of a variable

29, the global block can intercept variables, can only be global variables (can not be obtained to local variables). While global variables are inherently mutable in blocks, the "global block of intercepted variables" is equivalent to "intercept global blocks of mutable global variables".

30. Write the following code to execute in ARC mode:

The same code is executed in MRC mode:

As can be seen from the execution result, the global block storage area that intercepts the variable is the same as the normal global block. This shows that the global Block is __nsglobalblock__ class and stored in the. Data area.

Normal block

31, for ordinary block, write the following code to view its storage area, in Arc mode to print out the variables holding ordinary block, the copy value of the variable holding the ordinary block, the normal block object and the normal block object copy value:

The same code is also executed in MRC mode:

As can be seen from the printed results: in both ARC mode and MRC mode, the normal block that does not intercept the variable is treated as a __nsglobalblock__ class object, stored in the. Data area.

It's also important to note that the block object's Isa pointer is pointing to _nsconcretestackblock when the clang command is used to convert the normal block, which means that the normal block is essentially a __nsstackblock__ class. , and here by printing it is known that because there is no intercept variable, the compiler actually treated the ordinary block as a __nsglobalblock__ class.

Block that intercepts variables

32, the above demonstration of several blocks in different compiler mode storage area is not different, the next block will start to be different.

Write the following code to print out the variables that hold the Intercept variable block in arc mode, the copied value of the variable holding the block of the intercepted variable, the block object that intercepts the variable, and the copy value of the block object that intercepts the variable:

Then execute the same code in MRC mode to see the results:

Comparing the printing results in two modes, you can find:

(1), in MRC mode, the block object of the intercepted variable is stored on the stack itself, even if it is accessed by holding the variable of the block object, it can still access the block object stored on the stack. Only the Copy method can be actively called to copy the Block object from the stack to the heap;

(2), in Arc mode, the block object that intercepts the variable is stored on the stack itself, but when it is accessed using the variable holding the Block object, the block object is copied from the stack onto the heap, so that the variable accesses the block object stored on the heap. If the copy method is invoked actively, the block object is also assigned to the heap;

(3), comparing the differences between the two modes, you can find that in the case of using variables to access the Block object, in different compiler mode, the storage area of the block object is different. That's why there's no _nsconcretestackblock in arc mode.

Block that intercepts mutable variables

33, take a look at the last block, write the following code, in Arc mode, respectively, to hold the variables to intercept the variable block, the copy value of the variable that holds the variable block, the block object that intercepts the variable variable, and the copy value of the block object that intercepts the variable variable:

Then execute the same code in MRC mode to see the results:

It can be found that, like the block of the intercepted variable, the block object's storage area is different in different compiler modes when the variable is used to access the Block object. Then we can draw the following conclusions: (1), in different compiler mode, the Block of the intercepted variable is stored on the stack itself;

(2), when using variables to access this Block , MRC mode can access the objects on the stack, ARC mode access to the object is copied to the heap.

Lattice 16: The Block I understand (3)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.