Re: PROBLEM



"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.
.



Relevant Pages

  • Re: PROBLEM
    ... You're mixing up two things here: whether string ... tmp.c:1: warning: initialization discards qualifiers from pointer target type ... The problem with C compilers is that these options are not enabled by default. ... New C programmers do not know about them, ...
    (comp.std.c)
  • Re: a few doubts!
    ... an attempt to modify a string literal invokes undefined ... read-only memory segment, causing a trap whenever the program attempts ... Some compilers may have an an option to control whether string ... writing to string literals in the first place. ...
    (comp.lang.c)
  • Re: What does it mean
    ... i_reg_fname is not a string constant ... String constants are sequences of characters enclosed in double ... Some compilers have a switch controlling whether string literals are ...
    (comp.lang.c)
  • Re: Perfrom Thru
    ... MCP/AS COBOL compilers have eschewed the use of support ... libraries to handle complex logic such as can be produced by STRING, ... UNSTRING and INSPECT, in the belief that candidates for optimization are ... >>is increased in size by a single character, ...
    (comp.lang.cobol)
  • Re: Function Warning - Null Reference
    ... Warning 1 Function 'Dec2hms' doesn't return a value on all code paths. ... These include String, Integer, DateTime and Boolean ...
    (microsoft.public.dotnet.languages.vb)