First, before we look at the three types of blocks, we need to know one thing:

(Warm reminder: if you did not read my previous blog, some concepts are not clear to you, you may be difficult to understand, if you have read the previous blog, you read this blog as simple as cutting vegetables!)

Memory allocation for a program

The memory footprint of a C/C++ compiled program is divided into the following sections

Stack: Automatically allocated by the compiler to hold function parameter values, local variable values, etc. It operates like a stack in a data structure.

Heap: Usually allocated and released by the programmer. If not released by the programmer, the program may be reclaimed by the OS at the end of the program. Note that this is not the same thing as a heap in a data structure and is allocated in a similar way to a linked list.

Global area (static area static) : global variables and static variables are stored in the same area, initialized global variables and static variables in the same area, uninitialized global variables and uninitialized static variables in another area adjacent to the end of the program released by the system.

Literal constant area: Constant strings are placed here and released by the system at the end of the program

Program code area: The binary code that holds the function body.

I believe you are very clear about the above knowledge points of memory allocation, so let’s take a look

The type of the block

There are three types of blocks. You can call the class method or isa pointer to see the specific type, and eventually inherit NSBlock type

A block is an OC object, so we can call class to know what type it is

So you can see what I said earlier, a block is an OC object, and this NSGlobalBlock inherits from NSBlock, and all the other classes inherit from NSBlock, and then you can use this method to try out the parent classes of the other two blocks.

NSGlobalBlock, NSStackBlock, NSMallcBlock

This is the result of the runtime output, but it might compile a little differently, so let’s convert this file into a c++ file, which I have mentioned many times in my previous blog, so I won’t go into detail this time main.m -o main.cpp)

In fact, you will find that after compiling, the generated type is NSConcreteStackBlock. First, we must use the runtime as the rule, because there is a run in the compile process, which dynamically modifies your object with the Runtime. And actually use clang to c + + code, sometimes is not necessarily the final code, actually is a little difference, actually in LLVM 6.0 is the same as before, and probably will now generate an intermediate file, and is in the operation of the program is more accurate, don’t feel as if you have students to view source code, don’t be surprised, this is very normal, can participate There is not much difference.

Let’s move on

GlobalBlock is a StackBlock,MallocBlock is a heap block, and StackBlock is a StackBlock

What type is GlobalBlock? What type is MallocBlock? What type is StackBlock?

Look at my summary first, and we’ll look at it in detail

Okay, so the auto variable is clear? The auto variable is a variable that is automatically destroyed when the region it’s in is done. If you don’t know, take a look at my last blog post, which was very clear. Let’s look at each type of block one at a time

GlobalBlock type

Look at the code below

Static local variable a, global variable A, is not auto, so the above three cases are GlobalBlock, because we use GlobalBlock very little, so there is not much need to explain this

StackBlock type (emphasis)

A block that accesses the auto variable is a StackBlock.

Result of running under ARC

There should be pay attention to the classmate, result and conclusion of our above are different, because the environment is actually the ARC, ARC under actually do a lot of things for you (the why, the content is a little more, the back of the blog, I’ll detail, regardless you why this is so), so we change to MRC to see the essence of it (the Object directly Ive -c Automatic Reference Counting set to NO

Result of running under MRC

This is the StackBlock type. Let’s take a closer look at a special case of StackBlock, as shown below

Result of running under MRC

Notice how weird this age is. When the test() function finishes scoping, the variables on the stack in its scope will be destroyed. For example, the block inside the stack will be freed after all tests () have been executed, resulting in chaotic access. The whole block gets freed and everything in it gets messed up

How do they solve this problem?

The answer is obvious, let’s just turn block stack memory into heap memory. Wow! Copy is a stack block. Copy is a stack block.

Is it easy to solve! For those of you who are wondering, what if you also called copy with GlobalBlock? GlobalBlock, if MallocBlock is also called copy, what happens? You can verify this for yourself, the answer is again: reference count plus 1

And if we block access an object, an object is also an auto variable, so it’s the same thing, so I’m not going to validate that, so if you have any questions you can discuss it in the comments section.

MallocBlock type

That’s pretty clear on the top

I believe some students have questions. Just now, we tested it under MRC. As for why ARC is a different result, I will talk about it in my later blog.

I will continue my discussion of blocks by introducing some specifics and use of heap mallocblocks

If you find my writing helpful, please follow me and I will continue to update 😄