lifetime of compound literal

Discussion in 'C Programming' started by William Ahern, May 10, 2005.

  1. I can't figure out what the lifetime of a compound literal is; is it valid
    for the function or only for the block. For instance, is the following
    okay?

    struct example {
    char *str;
    };

    void func(void) {
    struct example *e;

    do {
    e = &(struct example){};
    e->str = (char []){ "hello world!" };
    } while(0);

    /* Is e undefined now? */

    printf("%s\n",e->str);

    }

    TIA,

    Bill
     
    William Ahern, May 10, 2005
    #1
    1. Advertising

  2. William Ahern

    Guest

    It should have the lifetime of the function func in this case.
     
    , May 11, 2005
    #2
    1. Advertising

  3. William Ahern wrote:

    > I can't figure out what the lifetime of a compound literal is; is it valid
    > for the function or only for the block.


    From the Standard:
    " If the compound
    literal occurs outside the body of a function, the object
    has static storage duration; otherwise, it has automatic
    storage duration associated with the enclosing block."

    So I don't think you can count on it being valid for the lifetime of the
    whole function.


    Christian
     
    Christian Kandeler, May 11, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWlrZUw=?=
    Replies:
    0
    Views:
    396
    =?Utf-8?B?TWlrZUw=?=
    Nov 19, 2004
  2. Luca Forlizzi

    array compound literal

    Luca Forlizzi, Feb 11, 2010, in forum: C Programming
    Replies:
    4
    Views:
    1,134
    Luca Forlizzi
    Feb 12, 2010
  3. Lauri Alanko

    Compound literal without initializer?

    Lauri Alanko, Feb 16, 2011, in forum: C Programming
    Replies:
    3
    Views:
    362
    Lauri Alanko
    Feb 16, 2011
  4. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    408
    Anonieko Ramos
    Sep 27, 2004
  5. Christopher Collins

    Size of a compound literal array

    Christopher Collins, Sep 12, 2013, in forum: C Programming
    Replies:
    1
    Views:
    230
    Eric Sosman
    Sep 12, 2013
Loading...

Share This Page