- NSNumber *number3 = @3;
- NSNumber *number4 = @4;
- NSNumber *number9 = @29;
- NSLog(@"number3 pointer is %p", number3);
- NSLog(@"number4 pointer is %p", number4);
- NSLog(@"number9 pointer is %p", number9);
- NSNumber *numberFFFF = @(0xFFFF);
- NSLog(@"numberffff pointer is %p", numberFFFF);
- NSNumber *bigNumber = @(0xEFFFFFFFFFFFFFFF);
- NSLog(@"bigNumber pointer is %p", bigNumber);
复制代码
运行结果:
- 2015-11-05 18:57:12.589 OCTest[860:128075] a: 0x100300600
- 2015-11-05 18:57:12.589 OCTest[860:128075] b: 0x100300610
- 2015-11-05 18:57:12.589 OCTest[860:128075] number3 pointer is 0x327
- 2015-11-05 18:57:12.589 OCTest[860:128075] number4 pointer is 0x427
- 2015-11-05 18:57:12.589 OCTest[860:128075] number9 pointer is 0x1d27
- 2015-11-05 18:57:12.589 OCTest[860:128075] numberffff pointer is 0xffff27
- 2015-11-05 18:57:12.589 OCTest[860:128075] bigNumber pointer is 0x100300650
复制代码
这是很诡异的事情:压根不知道是在数据区呢,还是栈区呢,还是堆区呢?除了最后一个,后两位都是“27”,而它之前则与该指针代表的值一样?巧合么?
其实它们已经不是我们所知道的NSNumber对象了,而是另外一个东西:Tagged Pointer
为什么会有这个东西,简而言之,为了节约内存
怎么节约内存?一个指针已经占了8个字节,它指向的NSNumber对象的值(8个字节)及其isa指针(8个字节),一共24个字节!
而Tagged Pointer,除了储存地址,还存储了实际的值,而且才占8个字节!少了2/3,访问速度和创建速度都大大提升了! |
|