Thought on an ANSI lint

Henry Spencer henry at utzoo.uucp
Wed Feb 22 02:14:27 AEST 1989


In article <1057 at vsi.COM> friedl at vsi.COM (Stephen J. Friedl) writes:
>     I am postulating that it would be helpful to have a lint
>option that make string literals "array of const char" rather
>than "array of char" and do type checking on that basis...

It sounds superficially attractive, but I don't think it would help.
All those pointers to const char would end up turning into pointers to
char as soon as they got used anywhere anyway, and the type checking
would be more of a hindrance than a help.  To run down instances of
string literals being modified, I'm afraid you need data-flow analysis,
and probably fairly sophisticated data-flow analysis.  There just is
no simple way around the problem.

>Committee apparently considered this for the language itself but
>eventually rejected it (maybe some ANSI-kind-of-people could
>comment on this).  Perhaps they rejected it because it would have
>required that old code be retrofitted...

In general (note this qualifier), X3J11 rejected anything that required
that old, legal, clean code be retrofitted.  One can argue about the
definitions of some of those words, but the intent was clear.

There is a basic problem with "const" in that people want it to mean
two different things:  constant (this really is a constant, which does
not change) and read-only (this may be a variable, but *you* are not
allowed to change it).  Trying to combine the two in one keyword leads
to many boobytraps.  For example, if const really means constant, then
surely casts from (say) "const char *" to "char *" should be illegal?
Unfortunately, if it means read-only, such casts are necessary, e.g.
to let strchr accept a "const char *" argument and return "char *".
At least one of the older drafts did have strings as arrays of const char,
but it just caused too many problems.  "Const" is trickier than it looks.
-- 
The Earth is our mother;       |     Henry Spencer at U of Toronto Zoology
our nine months are up.        | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.std.c mailing list