[comp.std.c] zero-sized objects

flaps@dgp.toronto.edu (Alan J Rosenthal) (06/17/90)

People don't seem to see the use of zero-sized objects.  So I thought I'd post
briefly the reason for them.  (Unfortunately, the standard rules them out.
*Sigh*, in the good old days (tm) I used to be able to say "char a[0];"...)

The basic reason is to avoid special-casing zero, which is not always a very
special case.  Many things in real life can be of size zero (with the obvious
exception of usenet articles).  For example, in the unix world, files can be of
zero size.  A simple program which reads in a file and transforms the whole
file in some way (e.g. reverses it) might want to malloc enough storage to hold
the whole thing.  How about something like (minor error flames to /dev/null,
this is just an example to get the idea across):

	if (fstat(fileno(fp), &statbuf))
	    complain;
	if ((p = malloc(statbuf.st_size)) == NULL)
	    complain;
	if (fread(p, statbuf.st_size, 1, fp) != 1)
	    complain;
	for (i = 0; i < statbuf.st_size / 2; i++) {
	    t = p[i];
	    p[i] = p[statbuf.st_size - 1 - i];
	    p[statbuf.st_size - 1 - i] = t;
	}
	if (fwrite(p, statbuf.st_size, 1, somewhere_else) != 1)
	    complain;

Now under the ansi standard this program is only guaranteed to work for
non-zero-sized files.  Quite a shame.

ajr

--
"IEEE 754 ... invents a new fantasy world of nonstandard mathematics ..."
					-- Doug Gwyn