Block static global local variables in iOS block variables, as well as objects, detailed!

Source: Internet
Author: User

//Recently always confused, about block on the external variables of the reference, today, there is time to write a bit, deepen their understanding, consolidate the basic knowledge




1 #import<Foundation/Foundation.h>2 intGnum = -;3 intMainintargcConst Char*argv[]) {4 @autoreleasepool {5 Static intSnum = -;6__blockintBnum = -;7 intLnum = -;8 int(^sum) (int,int) = ^(intAintb) {9NSLog (@"%d %d%d%d", snum,gnum,lnum,bnum);Tensnum++; Onebnum++; Agnum++; - returnA + B + lnum + gnum + snum +Bnum; - }; theSnum =1; -Bnum =1; -Gnum =1; -Lnum =1; + -NSLog (@"%d", SUM (1,2)); +NSLog (@"%d", snum); ANSLog (@"%d", bnum); atNSLog (@"%d", gnum); -NSLog (@"%d", lnum); - - } - return 0; -}




Print results

1  A: $:09.984xxx[10180:2208151]1092  A: $:09.984xxx[10180:2208151]23  A: $:09.984xxx[10180:2208151]24  A: $:09.984xxx[10180:2208151]25  A: $:09.984xxx[10180:2208151]1

Block actually contains two pieces of content
1.Block executes the code, which is generated at the time of compiling the good;
2. A data structure that contains all the variable values required by the block execution. The block will use the value of the variable near the scope to create a copy of the snapshot to the stack.
block is different from the function, where blocks are similar to OBJC objects that can be used to manage memory using an auto-release pool
3 block access management for external variables
Basic data Types
1, local variables
local automatic variable, read-only in block. The value of the copy variable when the block is defined is used as a constant in the block, so even if the value of the variable is changed outside the block, it does not affect his value in the block.
2, static variable or global variable of the static modifier
because global variables the address of the static variable in memory is fixed, the block reads the change value directly from the memory in which it is located, and gets Daode is the latest value, not the constant that was copied at the time of definition.
3,__block Modified Variables
A block variable, called a block variable, is a variable that is __block modified. Basic types of block variables such as drizzle global variables or static variables. The use of blocks is much like a function pointer, but the biggest difference with a function is that the block can access the values of external variables outside of the function, within the lexical scope.

Ii. Types of objects

Local objects

-(void) localobjtest  {      *localobj = [[UILabel alloc]init];      NSLog (@ "localobj adress--%p", & localobj);       void (^test) () = ^{          NSLog (@ "localobj adress--%p", & localobj);          NSLog (@ "localobj--%@", Localobj);      };       = Nil;      Test ();  }  

Printing results:

2018-Geneva- -  -: -:25.848test1[1332:79267] Localobj adress--0xbff7ced42018-Geneva- -  -: -:25.848test1[1332:79267] Localobj adress--0X7B63F8B42018-Geneva- -  -: -:25.848test1[1332:79267] Localobj--<uilabel:0x7c8337a0; frame = (0 0;0 0);

//Static Objects- (void) Staticobjtest {StaticUILabel *Staticobj; Staticobj=[[UILabel alloc]init]; NSLog (@"staticobj adress--%p", &staticobj); void(^test) () = ^{NSLog (@"staticobj adress--%p", &staticobj); NSLog (@"staticobj--%@", Staticobj);      }; Staticobj=Nil;  Test (); }   

Printing results:

2018-Geneva-17 -: -:25.917test1[1332:79267] Staticobjadress--0x85a482018-Geneva-17 -: -:25.917test1[1332:79267] Staticobjadress--0x85a482018-Geneva-17 -: -:25.917test1[1332:79267] Staticobj--(NULL)

//Global Objects- (void) globalobjtest {globalobj=[[UILabel alloc]init]; NSLog (@"staticobj adress--%p", &globalobj); void(^test) () = ^{NSLog (@"globalobj adress--%p", &globalobj); NSLog (@"globalobj--%@", Globalobj);      }; Globalobj=Nil;  Test (); }  

Printing results:

2018-Geneva- -  -: -:25.917test1[1332:79267] Staticobjadress--0x85a4c2018-Geneva- -  -: -:25.917test1[1332:79267] Globalobjadress--0x85a4c2018-Geneva- -  -: -:25.917test1[1332:79267] Globalobj--(NULL)

//Block Object- (void) blockobjtest {__block UILabel*blockobj =[[UILabel alloc]init]; NSLog (@"blockobj adress--%p", &blockobj); void(^test) () = ^{NSLog (@"blockobj adress--%p", &blockobj); NSLog (@"blockobj--%@", Blockobj);      }; Blockobj=Nil;  Test (); }  

Printing results:

2018-Geneva- -  -: -:25.917test1[1332:79267] Blockobj adress--0xbff7ced02018-Geneva- -  -: -:25.918test1[1332:79267] Blockobj adress--0x7c8355982018-Geneva- -  -: -:25.918test1[1332:79267] Blockobj--(NULL)

Summary: From the test results can be seen

* For static object variables and global object variables, the address is also not fixed.

* for local (local) object variables, when the block is defined, the (pointer) variable is also copied, which is used as a (pointer) constant in the block and unaffected by the outside world.

* For block object variables, it is also possible to define blocks to transfer variables from the stack to the heap, which can be verified by the same method. Therefore, the block variable is externally affected, causing the output to be empty.

Block static global local variables in iOS block variables, as well as objects, detailed!

Related Article

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.