🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #1441

zero-sized objects (was Re: fwrite(buf, 0, 42, stream) == ?)

#1441
From: Alan J Rosenthal
Date: Sat, 16 Jun 1990 17:35
35 lines
1798 bytes
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
Technical Details
Message-ID: <1990Jun16.133535.21986@jarvis.csri.toronto.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!dgp.toronto.edu!flaps
References: <Fkucoph@cs.psu.edu> <13094@smoke.BRL.MIL> <F.-&asi@cs.psu.edu> <1761@necisa.ho.necisa.oz>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!dgp.toronto.edu!flaps
Newsgroups: comp.std.c
From: flaps@dgp.toronto.edu (Alan J Rosenthal)
Subject: zero-sized objects (was Re: fwrite(buf, 0, 42, stream) == ?)
Message-ID: <1990Jun16.133535.21986@jarvis.csri.toronto.edu>
References: <Fkucoph@cs.psu.edu> <13094@smoke.BRL.MIL> <F.-&asi@cs.psu.edu> <1761@necisa.ho.necisa.oz>
Date: 16 Jun 90 17:35:36 GMT
Lines: 35