Advertisements

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)

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?

Advertisements

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.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!