block在内存中的分布
来源:互联网 发布:与权志龙合照软件 编辑:程序博客网 时间:2024/05/23 10:55
作者:Love@YR
链接:http://blog.csdn.net/jingqiu880905/article/details/50685683
请尊重原创,谢谢!
block 有三种类型 NSConcreteGlobalBlock、_NSConcreteStackBlock、_NSConcreteMallocBlock
分别存储在数据区,内存栈区,内存堆区
那么什么时候存储在哪个区上呢?我们看看下面的例子。
#import "ViewController.h"static NSString *globalStr=@"global";typedef int (^blk_t)(int);@interface ViewController (){}@end@implementation ViewController。。。省略- (IBAction)buttonTaped:(id)sender { int *a2; __block int b2; b2=3; a2=&b2; static NSString *localStr=@"local"; //未引用外部变量即为NSGlobalBlock blocakName bl1=^void(int a){ localStr=@"localstr1";//局部静态变量,通过指针传递 globalStr=@"globalStr1";//全局静态变量和全局变量,通过值传递 NSLog(@"a is %d,local str is %@,global str is %@",a,localStr,globalStr); }; NSLog(@"bl1 is at %@",bl1); blocakName bl2=^void(int a){ localStr=@"localstr1";//局部静态变量,通过指针传递 globalStr=@"globalStr1";//全局静态变量和全局变量,通过值传递 b2=6;//局部变量,b2需要__block修饰 不然只可读 NSLog(@"a is %d,a2 is %d,b2 is %d,local str is %@,global str is %@",a,*a2,b2,localStr,globalStr); }; //引用了外部变量即为stack block,block的实现与上面的完全相同 NSLog(@"bl2 block is %@",^void(int a){ localStr=@"localstr1";//局部静态变量,通过指针传递 globalStr=@"globalStr1";//全局静态变量和全局变量,通过值传递 b2=6;//局部变量,b2需要__block修饰 不然只可读 NSLog(@"a is %d,a2 is %d,b2 is %d,local str is %@,global str is %@",a,*a2,b2,localStr,globalStr); }); //引用了外部变量即为stackblock,arc下当引用此block变量时自动复制到堆上 NSLog(@"bl2 is %@",bl2); bl1(5);//调用 bl2(10);//调用}@end
打印的结果:
bl1 is <__NSGlobalBlock__: 0x10f13c0d0>bl2 block is <__NSStackBlock__: 0x7fff50ac3078>bl2 is <__NSMallocBlock__: 0x7fc81bf21aa0>a is 5,local str is localstr1,global str is globalStr1a is 10,a2 is 3,b2 is 6,local str is localstr1,global str is globalStr1
参考:http://www.cnblogs.com/hanjun/p/3767394.html
0 0
- block在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- 程序在内存中的分布
- mysql 5.6 rpm 包地址
- CSS的position
- 对象间的联动——观察者模式
- Java线程(十一):Fork/Join-Java并行计算框架
- 利用log4j+mongodb实现分布式系统中日志统一管理
- block在内存中的分布
- 2、基本运算
- 使用 Elasticsearch ik分词实现同义词搜索
- 【图形处理】如何将一个平面图形按照角度往z轴扭曲?
- 分享一个apns的项目
- 在Windows下使用curl命令
- hadoop I/O笔记
- ZOJ 3747 Attack on Titans
- tf卡数据恢复,手机里tf热插拔造成手机无法识别,TF卡电脑无法读取,提示使用前需要格式化