标签:
#import <Foundation/Foundation.h> typedef int(^block)(int, int); int d = 10; int main(int argc, const char * argv[]) { //1、block和局部变量 //局部变量在栈区 //block中的变量只能使用 不能修改 在内存中的堆区中复制一份 int c = 10; NSLog(@"c = %d", c); NSLog(@"c address = %p", &c); int (^b)(int, int) = ^int(int a, int b){ NSLog(@"c = %d", c); NSLog(@"c address = %p", &c); return 0; }; NSLog(@"%d",b(1, 2)); //2.__block和局部变量 //局部变量在栈区 //block区域使用的变量在堆区 __block int e = 10; NSLog(@"e = %d", e); NSLog(@"e address = %p", &e); int (^ee)(int, int) = ^int(int a, int b){ e++; NSLog(@"e = %d", e); NSLog(@"e address = %p", &e); return 0; }; NSLog(@"%d",ee(1, 2)); //3、block和全局变量 //全局变量在block中能够使用并且修改。 //全局变量在内存的静态代码区 //block块中的变量也在静态代码区 并且两个数的地址相同 NSLog(@"d = %d", d); NSLog(@"d address = %p", &d); int (^b2)(int, int) = ^int(int a, int b){ d++; NSLog(@"d = %d", d); NSLog(@"d address = %p", &d); return 0; }; NSLog(@"%d",b2(1, 2));
标签:
原文地址:http://my.oschina.net/zhangjie9142/blog/501286