编码:
__block int x = 0;
int *pointerToX = &x;
NSLog(@"x's location is on the stack: %p", &x); //stack
int (^block)() = ^{
x += 1;
return x;
};
NSLog(@"x's location is on the %@: %p", (&x == pointerToX ? @"stack" : @"heap"), &x);
block();
NSLog(@"x's location is on the %@: %p", (&x == pointerToX ? @"stack" : @"heap"), &x);
block = [block copy];
NSLog(@"x's location is on the %@: %p", (&x == pointerToX ? @"stack" : @"heap"), &x);
在非弧
x 的位置在堆栈上:0xbfffdba0
x 的位置在堆栈上:0xbfffdba0
x 的位置在堆栈上:0xbfffdba0
x 的位置在堆上:0x7195860
在弧中:
x 的位置在堆栈上:0xbfffdb70
x 的位置在堆栈上:0xbfffdb70
x 的位置在堆栈上:0xbfffdb70
x 的位置在堆上:0x7195860
为什么要在 arc 和 non-arc 中获得不同的输出?