KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I know that a __block variable will be moved to the heap from the stack if a Block accessing it was copied. But the following test code show me that the __block variable is moved to the heap before the Block's copying. That is, the four outputs are: stack => heap => heap => heap, which is not my expected result: stack => stack => stack => heap. Could someone straighten me out? __block int x = 0; int *pointerToX = &x; //1. It's on the stack NSLog(@"x's location is on the stack: %p", &x); int (^block)() = ^{ x += 1; return x; }; //2. I think its stack, but it's heap NSLog(@"x's location is on the %@: %p", (&x == pointerToX ? @"stack" : @"heap"), &x); //it's heap not stack block(); //3. I think its stack, but it's heap NSLog(@"x's location is on the %@: %p", (&x == pointerToX ? @"stack" : @"heap"), &x); //it's heap not stack block = [block copy]; // The variable x will be moved to the heap //4. I think its stack, but it's heap NSLog(@"x's location is on the %@: %p", (&x == pointerToX ? @"stack" : @"heap"), &x); //heap
Tags (comma-separated)
Save Edits
Cancel