Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocks, loops and local variables

Consider the following code fragment:

for(/* some condition */) {
   int x = rand();
   [array addObject:^(){
       NSLog(@"%d", x);
   }]
}

for(void (^block)() in array) {
    block();
}

Now I would expect this code snippet to print out all values assigned to x in that for loop; however it seems that all blocks share the same 'x' variable (presumably the last one).

Any idea why this is so and how I could fix the code to have each block contain the variable 'x' as it was at the time the block was defined?

like image 669
Mihai Damian Avatar asked Oct 13 '11 07:10

Mihai Damian


People also ask

Are variables in for loops local?

In Python, on the other hand, variables declared in if-statements, for-loop blocks, and while-loop blocks are not local variables, and stay in scope outside of the block.

Are variables in for loop local or global?

For the most part, loops do not have their own scope, so the variable's scope will be the scope of wherever the for loop lives. With that in mind, if the for loop is within a function, it will have local scope.

Which block create variables?

Variable block (counter)

What are local variables?

A local variable is a type of variable that can be used where the scope and extent of the variable is within the method or statement block in which it is declared. It is used as an iteration variable in the foreach statement, exception variable in the specific-catch clause and resource variable in the using statement.


1 Answers

The documentation specifically says not to do this. The reason is that blocks are allocated on the stack, which means they can go out of scope. For the same reason you can't access the variable x outside of the first for loop, you also shouldn't use that block. x has gone out of scope, along with the block itself, and could contain any value.

To get around this, you can take a copy of the block like so:

for(/* some condition */) {
   int x = rand();
   void(^logBlock)() = ^() { NSLog(@"%d", x); }
   [array addObject:[[logBlock copy] autorelease]];
}

This moves the block onto the heap, and should fix your problem.

like image 63
Tom Dalling Avatar answered Sep 28 '22 03:09

Tom Dalling