Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6122297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:52:56+00:00 2026-05-23T15:52:56+00:00

To understand the lexical scope of block, I have write the following code typedef

  • 0

To understand the lexical scope of block, I have write the following code

typedef int (^ MyBlock)(void);

MyBlock b[3];

for (int i=0; i<3; i++) {
    b[i]=^{return i;};
}

for (int i=0; i<3; i++) {
    NSLog(@"%d",b[i]());
}

NSLog(@"----------------------------");

int j=0;

b[0]=^{return j;};  
j++;

b[1]=^{return j;};
j++;

b[2]=^{return j;};

for (int i=0; i<3; i++) {
    NSLog(@"%d",b[i]());
}
  1. first time o/p is 2,2,2
  2. second time o/p is 0,1,2

I am expecting 2,2,2 for both of block execution.

Can anybody please explain me why is it so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:52:57+00:00Added an answer on May 23, 2026 at 3:52 pm

    I assume you’ve been reading bbum’s post on blocks and know that your code isn’t correct since you aren’t copying the blocks from the stack to the heap.

    That said:

    for (int i=0; i<3; i++) {
        b[i]=^{return i;};
    }
    

    does the following in each iteration:

    1. Allocates space in the stack for a block variable. Let’s say its memory address is A;
    2. Creates the block in the stack and assign its address (A) to b[i];
    3. At the end of the iteration, since the compound statement/scope ({}) has ended, pops whatever was in the stack and resets the stack pointer.

    The stack grows at the beginning of each iteration, and shrinks at the end of each iteration. This means that all blocks are being created in the same memory address, namely A. This also means that all elements in the b array end up pointing to the same block, namely the last block that was created. You can test 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 one 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’s no compound statement that defines the same scope for all blocks. This means that each time you create a block its address is further down the stack, effectively assigning a different address to each block. Since all blocks are different, they correctly capture the current runtime value of j.

    If you print the address of those blocks as described earlier, you should get an output similar to:

    0x7fff5fbff9b8
    0x7fff5fbff990
    0x7fff5fbff968
    

    showing that each block is at a different memory address.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to understand lexical-scoping. In lexical-scoping, I have this code, C like syntax:
I understand these are necessary...of course to write proper code, but is there a
I have written simple lexical analyzer. And I understand the need to provide each
I understand the principles but i have a hard time seeing where the practical
I have written a procedure to compute the sum of squares of the first
I understand that it is best practice to refactor as much of my code
I try to understand the VBA scope type, it's impossible to make this such
I understand that a const pointer can be declared a couple ways: const int
I understand that according to this issue ticket on google code http://code.google.com/p/fullcalendar/issues/detail?id=143 that there
I understand that sessions in HTTP is stateless and hence we have methods like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.