Life time of the string referenced by a __FILE__ preprocessor statement.

J

Johan Lindh

This is somewhat obscure, and I'm not sure if the question is valid in
comp.lang.c. If it isn't, please ignore it.

In a C program, the pointer resulting from a __FILE__ is stored.
The pointer refers to a normal string constant (I think).
Does the standard say anything about when that constant is destroyed,
thus rendering any use of the stored pointer undefined behavior?

I guess the question might be simplified into:

"What's the lifetime of a string constant?"

My gut reaction is of course "Until the program ends".

Regards,

Johan
 
J

Jeremy Yallop

Johan said:
In a C program, the pointer resulting from a __FILE__ is stored.
The pointer refers to a normal string constant (I think).

I'm not quite sure what you mean. __FILE__ is textually replaced with
a string literal (i.e. an anonymous null-terminated array of char).
I guess the question might be simplified into:

"What's the lifetime of a string constant?"

My gut reaction is of course "Until the program ends".

Right. A string literal object has "static" storage duration, which
means:

Its lifetime is the entire execution of the program and its stored
value is initialized only once, prior to program startup.
[C99 6.2.4]

Jeremy.
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top