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 7069879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:29:57+00:00 2026-05-28T05:29:57+00:00

Block variable in objective-c is a reference and I learned from somewhere infer that

  • 0

Block variable in objective-c is a reference and I learned from somewhere infer that following code may be transformed by compiler to another form.

Original code:

typedef int (^block_type)();
block_type create_k(int i)
{
    block_type block = ^(){
        return i;
    };
    //[block copy];
    return block;
}

Generated Code:

typedef void (*generic_invoke_funcptr)(void *, ...);
struct __block_literal {
    void *isa;
    int flags;
    int reserved;
    generic_invoke_funcptr invoke;
    struct __block_descriptor_tmp *descriptor;
    const int captured_i;
};
static const struct __block_descriptor_tmp {
    unsigned long reserved;
    unsigned long literal_size;
    /* no copy/dispose helpers needed */
} __block_descriptor_tmp = {
    0UL, sizeof(struct __block_literal)
};
// ^int (void) { return i; }
int __create_k_block_invoke_(struct __block_literal *bp) {
    return bp->captured_i;
}
typedef int (*iv_funcptr)(struct __block_literal *);

typedef int (^block_type)();
block_type create_k(int i)
{

    //block_type block = ^(){
    //    return i;
    //};
    struct __block_literal __b = {
        .isa = &_NSConcreteStackBlock,
        .flags = BLOCK_HAS_DESCRIPTOR,
        .reserved = 0,
        .invoke = (generic_invoke_funcptr)__f_block_invoke_,
        .descriptor = &__block_descriptor_tmp,
        .captured_i = i
    };
    struct __block_literal *block = &__b;
    return block;
}

So |_b| in stack and block is only a reference to |_b|. If |create_k| return |block|, the receiver only get a invalid address.

But

 int main(int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    printf("%d\n", create_k(40)());
    [pool drain];
    return 0;
}

By exec it, print |40| and |block| is a valid block. What’s the matter?

  • 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-28T05:29:58+00:00Added an answer on May 28, 2026 at 5:29 am

    My guess would be that the memory for that stack frame hasn’t been zeroed yet. Try calling another function between create_k() and printf() to get some other random data in there.

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

Sidebar

Related Questions

Short and sweet: Why does the following block of Objective-C code not produce a
I've come across Objective-C code that declares a variable right below the @implementation line
The following method does not work because the inner block declares a variable of
In Javascript, I have a variable that is set to a block of text
I've got some code with an apparent reference cycle in a block ivar. The
Consider the following code: // t included so block1 is a stack block. See
If I declare a variable within a block (see below) is there a way
So I've got a Ruby method like this: def something(variable, &block) .... end And
'Block' in the sense that the user is shown the Workflow is starting page
When copying and pasting a bit of sample code from MSDN, I came up

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.