Compiler warning

Discussion in 'C++' started by keith@bytebrothers.co.uk, Feb 13, 2008.

  1. Guest

    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?
     
    , Feb 13, 2008
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > 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.

    --
    Ian Collins.
     
    Ian Collins, Feb 13, 2008
    #2
    1. Advertising

  3. Guest

    On 13 Feb, 09:51, Ian Collins <> wrote:
    > wrote:
    > > 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.


    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?
     
    , Feb 13, 2008
    #3
  4. Guest

    On 13 Feb, 10:28, wrote:
    > On 13 Feb, 09:51, Ian Collins <> wrote:
    >
    >
    >
    > > wrote:
    > > > 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.

    >
    > 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.
     
    , Feb 13, 2008
    #4
  5. Guest

    > 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
     
    , Feb 13, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sravan reddy

    warning when using design compiler

    sravan reddy, Aug 14, 2005, in forum: VHDL
    Replies:
    0
    Views:
    491
    sravan reddy
    Aug 14, 2005
  2. Pete Becker
    Replies:
    0
    Views:
    1,379
    Pete Becker
    Feb 10, 2005
  3. B. Williams

    warning C4267 and warning C4996

    B. Williams, Oct 26, 2006, in forum: C++
    Replies:
    17
    Views:
    2,635
  4. WARNING! Prosoftstore.com is a SCAM! WARNING!

    , Jul 8, 2007, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    319
  5. Julian Mehnle
    Replies:
    17
    Views:
    886
    Julian Mehnle
    May 18, 2006
Loading...

Share This Page