Subject: Re: toolchain/22118: make won't compile with -Wcast-qual -Wstrict-prototypes and more
To: der Mouse <mouse@Rodents.Montreal.QC.CA>
From: Greg A. Woods <woods@weird.com>
List: tech-toolchain
Date: 07/17/2003 15:09:37
[ On Wednesday, July 16, 2003 at 20:42:34 (-0400), der Mouse wrote: ]
> Subject: Re: toolchain/22118: make won't compile with -Wcast-qual -Wstrict-prototypes and more
>
> > I don't know which references you've relied upon, but read-only
> > storage really must be const-qualified in order to make -Wconst-qual
> > warnings be of any use whatsoever.
>
> (a) Even if that were true, it's a gccism with little bearing on C.
No, it's not a GCCism. In ISO Standard C all string constants may be
stored in read-only storage and identical string constants may be stored
at the same address.
> (b) It's not true; there are people to whom -Wcast-qual (which is what
> I assume you meant when you wrote -Wconst-qual)
Yes, of course, that is indeed what I meant. Sorry for the confusion.
I also meant to include "-Wwrite-strings" since thats what really turns
on the warnings that help one find, at compile time, code that can try
to write into a string constant.
> is of use despite the
> presence of read-only storage that's not const-qualified.
With GCC string constants, even when used as initializers for pointers,
are stored in read-only storage by default and therefore are in fact
implied to be const qualified even if they're not always declared to be
const-qualified.
Finally note that it doesn't really matter what any standard or compiler
says or does about string constants used in expressions. They logically
must have the implied type of "const char *" because they _may_ be
stored in read-only storage and/or they _may_ be stored at the same
address as other identical string constants. Portable code must not
try to write to string constants if its author wishes to avoid it
producing undefined behaviour at runtime. Only by const-qualifying all
string constants and by strictly following all rules for 'cont' is it
possible to detect at compile time when such undefined behaviour may
occur at runtime.
Furthermore w.r.t. string constants as initializers in particular:
Even though as Ben has shown ISO C is at odds with GCC w.r.t. string
constants in expressions vs. intializers, it's irrelevant. GCC is the
most commonly used C compiler these days and it (currently) uses "const
char *" as the type for string constants in initializers so _portable_
code must assume this is true even if the ISO C Standard says it is not.
--
Greg A. Woods
+1 416 218-0098 VE3TCP RoboHack <woods@robohack.ca>
Planix, Inc. <woods@planix.com> Secrets of the Weird <woods@weird.com>