Josh Dybnis on 1 Oct 2003 05:41:51 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [ALACPP] Why won't g++ tell me I'm dumb?


C89 is the same as C99 on this point. But I was wrong about C++. The
C++ standard explicitly says string literals have type "array of n
const char" (section 2.13.4.1). But also retains that modifying a
string literal is "undefined" and not forbidden.

This does mean the original question is unanswered. The documentation
seems to be wrong about the behavior of gcc. Perhaps this behavior is a
bug in gcc. I tried it with the option -fconst-strings. It still
doesn't generate a warning. I would search in the gcc bug database
before sending off a bug report.

-Josh

--- Christopher Smith <x@xxxxxxxx> wrote:
> On Tue, 2003-09-30 at 21:42, Josh Dybnis wrote:
> > Technically gcc is doing the right thing here. String literals
> aren't
> > actually constants. It's pretty obscure. I had to actually bust out
> the
> > standard on this. What it says is that string literals are static
> > arrays of type char, not const char. Attempting to modify a string
> > literal is "undefined", but it's not actually forbidden for an
> > implementation to support it. This is from the C standard, but I
> assume
> > that C++ retains this for compatibility.
> 
> Out of curiosity, is this from the C89 or C99 standard?
> 
> I thought the relevant section of the gcc info page would help give
> you
> guys an idea as to why Kevin and I were surprised by this behavior:
> 
> `-fno-const-strings'
>      Give string constants type `char *' instead of type `const char
>      *'.  By default, G++ uses type `const char *' as required by the
>      standard.  Even if you use `-fno-const-strings', you cannot
>      actually modify the value of a string constant, unless you also
> use
>      `-fwritable-strings'.
> 
>      This option might be removed in a future release of G++.  For
>      maximum portability, you should structure your code so that it
>      works with string constants that have type `const char *'.
> 
> FYI, doing "-fwritable-strings" does of course fix this problem.
> 
> -- 
> Christopher Smith <x@xxxxxxxx>
> _______________________________________________
> alacpp mailing list
> alacpp@xxxxxxxxxxx
> http://lists.ellipsis.cx/mailman/listinfo/alacpp

_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp