[net.lang.c] A tuff question about strings

laman@sdcsvax.UUCP (Mike Laman) (08/13/83)

I don't think this got off this system, so I am resubmitting it.

There is a line in the System III source of "ed", that is like:

p = mktemp("/tmp/edXXXXXX");

Now as you may know "mktemp()" writes on the given argument.  I came
across a compiler that mapped strings into the linkage section.
Obviously, when "mktemp()" tried to write on the string, the program
died since the linkage section was write protected.  My question is
this:

What does "The C Programming Language" say about this problem?  Was
the compiler incorrect according to Kerningham and Ritchie?

I am NOT asking if people SHOULD do it or not.  That will be as
interesting a discussion as where people put their curly braces.
I just want to find what is correct.  You might as well submit it to the net;
this could be interesting.  I can't find the answer in the book.

				Mike Laman
				UUCP: {ucbvax,philabs,sdccsu3}!sdcsvax!laman
				ARPA: mike@NPRDC

johnl@ima.UUCP (08/17/83)

#R:sdcsvax:-364800:ima:15900010:000:760
ima!johnl    Aug 16 12:18:00 1983

The question is asked whether a C compiler that makes strings read-only
is in conformance with K&R..

It appears that it is indeed wrong.  The C reference in section 2.5 states
that strings are semantically static arrays of characters.  The discussion
of "static" in section 4 talks about variables, not constants.  You might
read the exact words a little differently, but the intention is pretty clear.

In practice, zillions of existing C programs expect to be able to write on
their strings, so that read-only strings will break programs regardless of
the moral status of the compiler.

John Levine, ima!johnl

PS:  There has been some discussion of a "readonly" keyword to mark data
that should be read-only, but I never heard of it being blessed at Bell.

jmc@root44.UUCP (John Collins) (08/17/83)

Personally, I think that constants in a program ought to be exactly that,
and not something which might be what they look like the first time through,
and something else the next time.  There is an old chestnut about assigning
to a Fortran argument which has been passed as a constant.  Some languages
(e.g. BCPL, and (in a different sense) COBOL) allow you to change 'goto's
whilst the program is running to make them 'goto' somewhere else. UGH!!

However K&R p181 says that strings are 'storage class static'.  I suppose
that does decide the issue.  I think we do need a syntax for 'readonly'
storage classes, both for strings and external/static stuff so we can stop
patching assembler output, however that subject has already been aired
rather a lot...

		John Collins
			....!vax135!ukc!root44!jmc

dalka@ihuxe.UUCP (08/17/83)

The "C Programming Language" book leaves many issues untouched.
This happens to be one of them. So essentially its up to the 
compiler implementor as to what to do. This is why "C" is not
as portable as people make it out to be......

					Ken Dalka
					ihuxe!dalka

msm@syteka.UUCP (Michael S. Maiten) (09/07/83)

The iNTEL 186 C compiler (really Mark Williams C) has an option specified
when invoking the compiler that will put ALL strings into the constant segment
(which can be a section of ROM addressed by the 186 data-segment register).



					Michael S. Maiten
					Silicon Gulch, California
					<...!{ucbvax!menlo70,decvax}!sytek!msm>