[net.lang.c] Readonly variables

minow@decvax.UUCP (Martin Minow) (08/18/83)

The C language specification specifically requires that
strings be stored in writable memory.  This is unfortunate
in many applications (such as stand-alone real-time
control systems) where RAM space is limited (and initializing
RAM contents on power-up doubly expensive).

It also prevents string space compression (for example, if
strings were read-only, "bar" and "foobar" would share
the same storage space).  Furthermore, if strings were
read-only, they could be stored in "code" space, allowing
short displacement operations in certain cases.  On paging
systems, string space need not be written back to disk.

It would be nice to have a compiler directive that stored
constant strings in read-only memory, in addition to
the readonly keyword.

Note, that in constructions such as

	char	*foo = "bar";

There are two pieces of memory being initialized, the vector of
char's ('b', 'a', 'r', '\000') and the pointer to the 'b'.

	readonly char *foo = "bar";

would change the allocation characteristic of 'foo' but not
necessarily "bar".

In a large C program that will run in a stand-alone computer,
we "solved" the problem by a terribly ugly hack:  we defined
three macros that generated "magic cookies" in the assembly-
language output file.  A post-pass program ate the cookies,
generating appropriate program section directives.  The
macros were as follows:

	ROM_SECT(_C_Dnn)	The following is read-only data
	RAM_SECT(_C_Dnn)	The following is read-write data,
				    initialized to zero on powerup.
	IRAM_SECT(_C_Dnn)	The following is initialized
				    read-write data.

'nn' in the above was a unique integer.  These macros generated
something like:

	static int _C_Dnn = 1;	1, 2, 3 signal the "flavor" of the
				    section to be generated.

It would be nice to have this in the language.


Martin Minow
decvax!minow

hamilton@uiucuxc.UUCP (08/22/83)

#R:decvax:-16900:uiucuxc:21000004:000:115
uiucuxc!hamilton    Aug 21 17:22:00 1983

if the extra storage for the pointer in:
	char *foo = "bar";
bothers you, use:
	char foo[] = { "bar" };
instead...