I assume that you read bbums post on blocks and know that your code is wrong as you are not copying blocks from the stack to the heap.
That said:
for (int i=0; i<3; i++) { b[i]=^{return i;}; }
performs the following at each iteration:
- Allocates space on the stack for a block variable. Let say that his memory address: A;
- Creates a block on the stack and assigns its address (A)
b[i] ; - At the end of the iteration, since the compound statement / region (
{} ) has ended, everything that was on the stack pops up and resets the stack pointer.
The stack grows at the beginning of each iteration and contracts at the end of each iteration. This means that all blocks are created in the same memory address, namely A. It also means that all elements of the array b ultimately point to the same block, namely the last block that was created. You can verify this by running the following code:
for (int i = 0; i < 3; i++) { printf("%p", (void *)b[i]); }
which should output something like:
0x7fff5fbff9e8 0x7fff5fbff9e8 0x7fff5fbff9e8
All elements point to the same block, the last of which is created in the memory address A = 0x7fff5fbff9e8.
On the other hand, when you do the following:
b[0]=^{return j;}; j++; b[1]=^{return j;}; j++; b[2]=^{return j;};
There is no compound statement that defines the same area for all blocks. This means that every time you create a block, its address is further on the stack, effectively assigning a different address for each block. Since all blocks are different, they correctly capture the current value of the execution time j .
If you print the address of these blocks as described above, you should get a result similar to:
0x7fff5fbff9b8 0x7fff5fbff990 0x7fff5fbff968
indicating that each block has a different memory address.
user557219
source share