read-only strings?

Henry Spencer henry at utzoo.UUCP
Thu Oct 18 06:47:01 AEST 1984


I'm told that some members of the ANSI C committee would appreciate input
on one particular proposal that was brought up in the latest meeting and
will be brought up again in the next one (December):  what about making
"string constants" really constant?

Both K&R and the current ANSI C draft state, essentially, that literal
strings must be in writeable storage.  Sharing of text between different
strings is explicitly forbidden as an implementation tactic.  The draft
standard provides for casting strings to "const" to force them to be
read-only, but this is decidedly clumsy.

*Most* strings are in fact never written on, and might as well be
constant.  Most writes into literal strings are probably errors.  Berklix
even provides a post-processing tool to explicitly make strings read-only.

The proposal specifically exempts the special case of using a string
literal to initialize an array of char, where the string is used only
as a notational convenience.

The trouble is, *some* programs rely on being able to write into strings.
Invocations of mktemp(3) are perhaps the canonical example.  It can be
argued that casting to const is a more appropriate tactic.  The proposal
certainly represents a change to the language.

It comes down to cost/benefit ratio.  The benefits are basically greater
efficiency.  Especially (but not exclusively!) for the poor people working
on ROM-based micro systems, who currently have to use some of their scarce
RAM to hold stuff that could easily go in ROM.  The costs are the existing
programs that would break.  The benefits seem substantial; the costs are
*conjectured* to be small.  What do people think?  I like the idea myself.

Several members of the committee do read this newsgroup, so your views
won't go unnoticed.  Specific inquiries should go to Larry Rosler (the
chairman of the language subcommittee) at ihnp4!btlunix!lr .
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry



More information about the Comp.lang.c mailing list