existance-scope of struct literals

J

John Reye

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.
 
J

James Kuyper

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.
 
T

Tim Rentsch

John Reye said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top