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 

Warning about defined but not used for global variable

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Eric Lilja
Guest





PostPosted: Wed Jan 26, 2005 12:18 pm    Post subject: Warning about defined but not used for global variable Reply with quote



Hello, I have a few global variables in my program. One of them holds the
name of the application and it's defined in a header file globals.hpp (and
the point of definition also happen to be the point of declaration of this
variable, correct?):

static const char * g_application_name = "Tiny, class-based MDI Example";

In another source file, I'm including the header globals.hpp and I'm using
the variable g_application_name. I do, however, get the following warning
when building my program:
globals.hpp:12: warning: 'g_application_name' defined but not used

This warning is repeated for every file that includes globals.hpp, either
directly or indirectly.

However, if I change the type of g_application_name to const char * const,
the warning disappears.

So I guess that the proper way to define a global string variable that is
not to be written, only read, and should be shared for all source files is
to the define it with the type const char * const?

/ Eric


Back to top
alexmdac@hotmail.com
Guest





PostPosted: Wed Jan 26, 2005 12:30 pm    Post subject: Re: Warning about defined but not used for global variable Reply with quote



Yes you should use const char * const. const char * means that the
string buffer is writable but the pointer itself is constant. You
should treat string literals as read-only.

Back to top
Matthias
Guest





PostPosted: Wed Jan 26, 2005 1:17 pm    Post subject: Re: Warning about defined but not used for global variable Reply with quote



[email]alexmdac (AT) hotmail (DOT) com[/email] wrote:
Quote:
Yes you should use const char * const. const char * means that the
string buffer is writable but the pointer itself is constant. You
should treat string literals as read-only.


Hm isn't it the other way around? AFAIK const char* means that the
character data is constant, but the pointer to it isn't, whereas char*
const means that the character data is variable and the pointer constant.
Correct me if I'm wrong.

--
Regards,
Matthias

Back to top
scott urban
Guest





PostPosted: Sun Jan 30, 2005 5:22 pm    Post subject: Re: Warning about defined but not used for global variable Reply with quote

On 2005-01-26, Eric Lilja <ericliljaNoSpam (AT) yahoo (DOT) com> wrote:
Quote:
Hello, I have a few global variables in my program. One of them holds the
name of the application and it's defined in a header file globals.hpp (and
the point of definition also happen to be the point of declaration of this
variable, correct?):

static const char * g_application_name = "Tiny, class-based MDI Example";

Lose the static keyword; with static you get a separate variable named
'g_application_name' local to each translation unit that includes that
header - not 'global' at all. Change it to extern:

extern const char * g_application_name;

And, in one cpp file:

const char * g_application_name = "Tiny, class-based MDI Example";

Quote:
In another source file, I'm including the header globals.hpp and I'm using
the variable g_application_name. I do, however, get the following warning
when building my program:
globals.hpp:12: warning: 'g_application_name' defined but not used

This warning is repeated for every file that includes globals.hpp, either
directly or indirectly.
However, if I change the type of g_application_name to const char * const,
the warning disappears.

I'm not sure what the rules are here - but for types like int:

const int name = 0;

in a header is used everywhere possibly in read only memory and doesn't
need to be set up like a global variable that might change. That
apparently applies to 'const char * const' too, at least for your
compiler.

Quote:
So I guess that the proper way to define a global string variable that is
not to be written, only read, and should be shared for all source files is
to the define it with the type const char * const?

I think I would still do it the proper way, with a an extern declaration
in the header and a single definition in one cpp. At a minimum, if you
change the name of your application, you won't have to recompile all
files that include the header.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) 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.