Question about Block capturing variables

Local auto-value type variables (i.e. Int a = 10, the auto modifier is omitted by default), block is captured as value passing (deep copy) :

    int a = 10;
    NSLog(@" viewdidLoad int a address: %p",&a);
    // Two value type variables have different addresses
    BlockFunc blockFunc3 = ^(NSString *msg){
        NSLog(@" MSG int a address: %p",&a);
    };
Copy the code

Cause: Local auto-value type variables are destroyed when out of scope, so blocks need to be deeply copied

Local automatic pointer to type variables (i.e., ‘NSObject *obj = [NSObject new]’, with the auto modifier omitted by default), blocks are captured by value passing (deep copy) :

The memory address of the pointer variable of the same name in the block has changed, but its contents (that is, the address of the object to which it points) remain the same:

    BlockModel *bm = [BlockModel new];
    NSLog(@"viewdidload bm location:%p",&bm);
    // Two pointer type variables have different addresses
    BlockFunc blockFunc1 = ^(NSString *msg){
        NSLog(@"blockFunc1 bm location:%p",&bm);
    };
Copy the code

So the object [NSObject New] reference count is increased (+2 I don’t know why)

A local static value type variable (static int a = 10, declared in the local scope of a function) is captured by a block passing by reference:

    static int b = 10;
    NSLog(@" viewdidLoad int b address: %p",&b);
    // Both value type variables have the same address
    BlockFunc blockFunc4 = ^(NSString *msg){
        NSLog(@" MSG int b address: %p",&b);
    };
Copy the code

A local static pointer type variable (i.e. static NSObject *obj = [NSObject new]) is captured by a block passing by reference:

    static BlockModel *bm2 = nil;
    bm2 = [BlockModel new];
    NSLog(@"viewdidload bm2 location:%p",&bm2);
    // Both pointer variables have the same address
    BlockFunc blockFunc5 = ^(NSString *msg){
        NSLog(@"blockFunc5 bm2 location:%p",&bm2);
    };
    blockFunc5(@"hello");
Copy the code

So the reference count for the object [NSObject New] is constant

Here’s a word about Pointers and references

Variable a, then a represents its stored value, &a represents its memory address, passing the reference to give the original variable an alias, &c=a, then c and A are the same except for the name

Pointer to b, then B itself is a variable, but the value that this variable stores is a memory address,

Here’s something more to say about printing memory addresses

Both C and OC are printed with the placeholder %p, and the content needs to be the address, that is, &a if the variable name is a

We already know that a pointer is a variable, like NSObject *obj = [NSObject new]. To print the memory address of the pointer obj itself, you need NSLog(@”%p”,&obj), and to print the address of the pointer to an object, you need NSLog(@”%p”,obj), Because the variable obj represents the value it stores, which is the address to the object

One more note about initializing static variables

Static int a = 1; static int a = 1; static int a = 1; static int a = 1; But you cannot define static NSObject *obj = [NSObject new] because [NSObject new] is only executed at runtime, so you cannot determine the size of memory at compile time