Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > existance-scope of struct literals

Reply
Thread Tools

existance-scope of struct literals

 
 
John Reye
Guest
Posts: n/a
 
      05-01-2012
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.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      05-01-2012
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.
 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      05-08-2012
John Reye <(E-Mail Removed)> 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/wg...docs/n1256.pdf
http://www.open-std.org/jtc1/sc22/wg...docs/n1570.pdf

If not, get them!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling mechanisms and struct literals James Harris C Programming 1 11-27-2012 03:59 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Java: byte literals and short literals John Goche Java 8 01-17-2006 11:12 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments