existance-scope of struct literals

Discussion in 'C Programming' started by John Reye, May 1, 2012.

  1. John Reye

    John Reye Guest

    Is the existance-scope of struct literals, identical to that of
    declared variables?

    i.e. the "existance"-scope goes until the end of the current { } -
    delimited block, or the scope is global if it's outside any block.


    Example:


    #include <stdlib.h>

    struct y
    {
    struct y *num;
    int a;
    };

    struct x
    {
    struct y *x;
    };

    #define x(num) (&(struct y){NULL, (num)}) /* macro generates a struct
    literal and returns a pointer to it */

    int main(void)
    {
    int i;
    struct y *tmp;
    {
    tmp = x(1);
    }
    /* here the storage at pointer tmp cannot be accessed anymore,
    right? The literal struct is no longer in scope, right?*/
    return EXIT_SUCCESS;
    }


    PS:
    How does one officially call (what I have irreverently termed...)
    "existence"-scope?

    Thanks.
    John Reye, May 1, 2012
    #1
    1. Advertising

  2. John Reye

    James Kuyper Guest

    On 05/01/2012 05:29 PM, John Reye wrote:
    > Is the existance-scope of struct literals, identical to that of
    > declared variables?
    >
    > i.e. the "existance"-scope goes until the end of the current { } -
    > delimited block, or the scope is global if it's outside any block.


    I presume that you're using "struct literal" to refer to compound
    literals of struct type?

    I think that what you mean by "existence-scope" corresponds to what the
    C standard describes as an object's "lifetime": "the portion of program
    execution during which storage is guaranteed to be reserved for it"
    (6.2.4p2).

    "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." (6.5.2.5p5)

    If an object has static storage duration, "Its lifetime is the entire
    execution of the program" (6.2.4p3). If "it has automatic storage
    duration associated with the enclosing block", then "its lifetime
    extends from entry into the block with which it is associated until
    execution of that block ends in any way" (6.2.4p6)

    > Example:
    >
    >
    > #include <stdlib.h>
    >
    > struct y
    > {
    > struct y *num;
    > int a;
    > };
    >
    > struct x
    > {
    > struct y *x;
    > };
    >
    > #define x(num) (&(struct y){NULL, (num)}) /* macro generates a struct
    > literal and returns a pointer to it */
    >
    > int main(void)
    > {
    > int i;
    > struct y *tmp;
    > {
    > tmp = x(1);
    > }
    > /* here the storage at pointer tmp cannot be accessed anymore,
    > right? The literal struct is no longer in scope, right?*/


    The scope and the lifetime both come to an end at the same place; but
    that's true only for objects with automatic storage duration. In the
    more general case, the scope and the lifetime are two different things.
    for instance, if you had written:

    {
    static struct y z;
    tmp = &z;
    }

    the pointer value stored in tmp would still be usable, because the
    object 'z' has a lifetime that extends to the end of the program, even
    though 'z' is no longer in scope.

    > return EXIT_SUCCESS;
    > }
    >
    >
    > PS:
    > How does one officially call (what I have irreverently termed...)
    > "existence"-scope?


    See above.
    James Kuyper, May 1, 2012
    #2
    1. Advertising

  3. John Reye

    Tim Rentsch Guest

    John Reye <> writes:

    > Is the existance-scope of struct literals, identical to that of
    > declared variables? [snip]


    Yes. To use more accurate language, the lifetime of a compound
    literal is the same as a variable declared at "the same place"
    that the compound literal appears. (Compound literals don't
    have a scope per se, since there is no way of referencing them
    except as, well, literals.)

    Fine point: only for identifiers that are not variable length
    arrays, which have different rules (and may help explain
    why compound literals are prohibited from having a variable
    length array type.

    Do you have these?

    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

    If not, get them!
    Tim Rentsch, May 8, 2012
    #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. Ajit
    Replies:
    5
    Views:
    1,035
    Nicolas
    Sep 26, 2003
  2. John Goche
    Replies:
    8
    Views:
    16,448
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,370
    Chris Fogelklou
    Apr 20, 2004
  4. ociardhp

    Clarifying scope of literals

    ociardhp, Oct 25, 2007, in forum: Javascript
    Replies:
    6
    Views:
    137
    ociardhp
    Oct 26, 2007
  5. James Harris

    Calling mechanisms and struct literals

    James Harris, Nov 27, 2012, in forum: C Programming
    Replies:
    1
    Views:
    266
    Ben Bacarisse
    Nov 27, 2012
Loading...

Share This Page