Compiler warning

K

keith

I have always been perfectly comfortable passing a string literal to a
function which expects either a 'char*' or a 'const char *', both in C
and C++, and I've never seen any warning about this, until today when
I built my library on a Solaris machine, the Sun CC compiler gives me:

Warning: String literal converted to char* in formal argument fmt in
call to LOG::print

Can someone enlighten me why this compiler (version string "CC: Sun C+
+ 5.5 2003/03/12") thinks there may be something wrong with that?
 
I

Ian Collins

I have always been perfectly comfortable passing a string literal to a
function which expects either a 'char*' or a 'const char *', both in C
and C++, and I've never seen any warning about this, until today when
I built my library on a Solaris machine, the Sun CC compiler gives me:

Warning: String literal converted to char* in formal argument fmt in
call to LOG::print

Can someone enlighten me why this compiler (version string "CC: Sun C+
+ 5.5 2003/03/12") thinks there may be something wrong with that?

While passing a string literal to a function with a char* parameter is
permitted, it still isn't a very good idea...

The compiler is free to issue a diagnostic and it does. Good thing to
in my opinion.
 
K

keith

While passing a string literal to a function with a char* parameter is
permitted, it still isn't a very good idea...

The compiler is free to issue a diagnostic and it does. Good thing to
in my opinion.

Thanks for that, but I'm still unclear _why_ it is a bad idea. I
mean, people have been passing "hello world" as the first parameter to
printf(const char*, ...) for years with no warnings being seen. What
is it in the C++ world that makes it a bad idea to do this?
 
K

keith

Thanks for that, but I'm still unclear _why_ it is a bad idea. I
mean, people have been passing "hello world" as the first parameter to
printf(const char*, ...) for years with no warnings being seen. What
is it in the C++ world that makes it a bad idea to do this?

Sorry, sorry, sorry! I just looked more carefully, and it was giving
that warning only where I was passing a string literal to a function
expecting a "char*". When I changed the called function to expect a
"const char*", the warning went away, so it was basically warning me
about const-incorrectness.
 
S

sean_in_raleigh

Thanks for that, but I'm still unclear _why_ it is a bad idea. I
mean, people have been passing "hello world" as the first parameter to
printf(const char*, ...) for years with no warnings being seen. What
is it in the C++ world that makes it a bad idea to do this?

The reason it's dangerous is that it permits the following:

void foo(char *s) { s[0] = 'j'; }
...
foo("hello");

So you must ask yourself what memory is getting modified
in the function? It's up to the compiler, which will routinely
do things like coalesce all identical string literals into
one place, put them in read-only memory, etc.

Why don't most C compilers complain about it? Because C
programmers live on the edge.

I see that gcc has the following option:

`-fwritable-strings'
Store string constants in the writable data segment and don't
uniquize them. This is for compatibility with old programs which
assume they can write into string constants.
Writing into string constants is a very bad idea; "constants"
should be constant.


Sean
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top