Re: PROBLEM
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 21 Jul 2006 08:04:09 GMT
"Charlie Gordon" <news@xxxxxxxxxxx> writes:
"Keith Thompson" <kst-u@xxxxxxx> wrote in message[...]
news:lnirlrx4zy.fsf@xxxxxxxxxxxxxxxxxx
[...]In fact, gcc's "-Wwrite-strings" option does exactly this.
If string literals were const, the above would be a constraint
violation, requiring a diagnostic; it doesn't actually require it to
be rejected. By default, gcc with "-Wwrite-strings" merely prints
a warning:
tmp.c:1: warning: initialization discards qualifiers from pointer
target type
(Of course, such warnings can still be a nuisance, but it's not a bad
idea to compile existing code with "-Wwrite-strings" to find possible
bugs where the program *might* attempt to modify a string literal.)
If you add "-Werror", the warning becomes fatal.
Exactly my point!
New C code should be compiled with these options enabled.
The problem with C compilers is that these options are not enabled
by default. New C programmers do not know about them, nor should
they. New C code produced by new C programmers (supermen or not)
therefore reproduces old detestable habits, and their customary
cohorts of related bugs. This could be avoided, and C practice
would be made safer, by changing the Standard only slightly,
confining obsolete bug prone historical things in a compatibility
box only selected on option. Porting existing code to new compilers
would merely require very small changes such as setting or modifying
an environment variable CFLAGS_DEFAULT or touching up Makefiles or
configuration scripts. As a matter of fact, code configured for
--std=c89, needs no further adjustments when moving from c99 to c0x.
Backwards compatibility is a tough issue, with potentially
exhorbitant costs, but fanatical conservatism is not the only way to
address it.
Compilers are allowed, but not required, to have multiple modes, some
of them conforming, some possibly not. What you're proposing would
practically require all C compilers to implement a particular
non-conforming mode. And each compiler would have a different method
to invoke that mode. You mention "setting or modifying an environment
variable CFLAGS_DEFAULT or touching up Makefiles or configuration
scripts"; there are gazillions of other possibilities, and anyone
trying to build or distribute existing software on multiple platforms
would have to learn all of them.
Compiling new C code in a mode that flags possible writes to string
literals isn't a bad idea. Given gcc's "-Wwrite-strings" option, we
already have a tool do to exactly that. Note, however, that there's
plenty of code that doesn't actually modify string literal that would
become illegal if the language were modified to make string literals
const.
Certainly if the language were being designed from scratch today, with
no need to worry about existing code, it would make sense for string
literals to be treated as const. But we don't have that luxury.
Making such a change would undoubtedly have benefits, especially for
new code, but it would also have real and significant costs.
Incidentally, I don't recall anyone ever being persuaded to change his
mind by being accused of "fanatical conservatism". I try to keep in
mind that not everyone who disagrees with me is stupid.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- Follow-Ups:
- Re: PROBLEM
- From: Douglas A. Gwyn
- Re: PROBLEM
- References:
- Re: PROBLEM
- From: Charlie Gordon
- Re: PROBLEM
- From: Douglas A. Gwyn
- Re: PROBLEM
- From: Charlie Gordon
- Re: PROBLEM
- From: Hans-Bernhard Broeker
- Re: PROBLEM
- From: Keith Thompson
- Re: PROBLEM
- From: Charlie Gordon
- Re: PROBLEM
- Prev by Date: Re: PROBLEM
- Next by Date: Re: PROBLEM
- Previous by thread: Re: PROBLEM
- Next by thread: Re: PROBLEM
- Index(es):
Relevant Pages
|