C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Lifetime of the __FILE__ string literal

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthias Hofmann
Guest





PostPosted: Mon Aug 29, 2005 11:21 am    Post subject: Lifetime of the __FILE__ string literal Reply with quote



Hello!

I am writing a memory tracker which associates pointers with some allocation
information. The name of the source file will be part of that information,
which is predefined by the __FILE__ macro, as described in 16.8/1.

However, I found no information about the lifetime of this string literal,
so I don't know if it's safe to use a const char*:

struct Info
{
const char* file;
...
};

As the allocation information can be accessed any time from anywhere in the
programm, it would probably be better to use a std::string, but that
involves copying __FILE__, which might have some bad influence on the
performance. This is not that bad, as I am planning to track the memory only
in debug builds and switch it off in the release version, but I also would
not mind if I could avoid that problem.

Can anyone please tell me more about the lifetime of __FILE__?

--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Kurt Stege
Guest





PostPosted: Mon Aug 29, 2005 1:55 pm    Post subject: Re: Lifetime of the __FILE__ string literal Reply with quote



Matthias Hofmann wrote:

Quote:
... which is predefined by the __FILE__ macro, as described in 16.8/1.

However, I found no information about the lifetime of this string literal,
so I don't know if it's safe to use a const char*:

Can anyone please tell me more about the lifetime of __FILE__?

__FILE__ is a macro that creates a string literal.

That is,
const char *file = __FILE__;
has the same semantics as
const char *file = "/usr/home/myProjects/testfile.cpp";

The lifetime is the same as for all other string literals: forever.

HTH,
Kurt.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
srez
Guest





PostPosted: Mon Aug 29, 2005 4:37 pm    Post subject: Re: Lifetime of the __FILE__ string literal Reply with quote



Matthias,

In C++ string literals are defined to have static storage duration
(2.13.4 /1).

Regards,
srez


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.