lifetime of compound literal

W

William Ahern

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
 
C

Christian Kandeler

William said:
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
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top