[comp.std.c] fwrite

flee@psuvax1.cs.psu.edu (Felix Lee) (06/13/90)

What is fwrite(buf, 0, 42, stream) supposed to return?  I feel it
should return 42 when it successfully writes 42 objects of zero size,
but existing implementations seem to return 0.
--
Felix Lee	flee@cs.psu.edu

gwyn@smoke.BRL.MIL (Doug Gwyn) (06/13/90)

In article <Fkucoph@cs.psu.edu> flee@psuvax1.cs.psu.edu (Felix Lee) writes:
>What is fwrite(buf, 0, 42, stream) supposed to return?  I feel it
>should return 42 when it successfully writes 42 objects of zero size,
>but existing implementations seem to return 0.

Strictly speaking, there are no semantics defined for such usage,
because the "0" is supposed to be the size of some member of an
array, and there are no 0-sized objects in C.

flee@psuvax1.cs.psu.edu (Felix Lee) (06/14/90)

Doug Gwyn wrote:
>Strictly speaking, there are no semantics defined for such usage,

Well, does that mean it's undefined or implementation defined?  If the
standard defines malloc(0), why not fwrite(p, 0, n, stream)?

The reason I asked, it was natural for me to write a substring with
a)	if (fwrite(p, end - p, 1, stream) < 1) return ERR;
which runs into problems when p == end.

The alternative
b)	if (fwrite(p, 1, end - p, stream) < end - p) return ERR;
also has problems when p == end, since you can't distinguish failure
from success.

I have to use something like
c)	if (p < end && fwrite(p, end - p, 1, stream) < 1) return ERR;
or
d)	if (p < end && fwrite(p, 1, end - p, stream) < end - p) return ERR;

Somehow, I liked (a) best.  Failing that, I'm using (c).

Urmf.  If sizeof(char [n]) != n, then I can't use (c) either.
--
Felix Lee	flee@cs.psu.edu

gwyn@smoke.BRL.MIL (Doug Gwyn) (06/15/90)

In article <F.-&asi@cs.psu.edu> flee@psuvax1.cs.psu.edu (Felix Lee) writes:
>Doug Gwyn wrote:
>>Strictly speaking, there are no semantics defined for such usage,
>Well, does that mean it's undefined or implementation defined?  If the
>standard defines malloc(0), why not fwrite(p, 0, n, stream)?

It doesn't define semantics for malloc(0) either, for exactly the same
reason (the argument is the size of an object, and there are no zero-
sized objects).  The only requirement is that malloc() return either a
pointer to at least zero bytes of (distinguishable) allocated storage
or that it return a NULL pointer; the implementation may choose to do
the latter.

As POC for the zero-sized object extensions special interest group,
I would be happier if semantics for dealing with zero-sized objects
had been included in the standard, but a deliberate decision was made
to outlaw them.

>Urmf.  If sizeof(char [n]) != n, then I can't use (c) either.

But sizeof(char[n]) is n, since the byte (char) is the basic unit of
storage in standard C and sizeof(char)==1 is a requirement.

boyd@necisa.ho.necisa.oz (Boyd Roberts) (06/15/90)

In article <F.-&asi@cs.psu.edu> flee@psuvax1.cs.psu.edu (Felix Lee) writes:
>Doug Gwyn wrote:
>>Strictly speaking, there are no semantics defined for such usage,
>
>Well, does that mean it's undefined or implementation defined?  If the
>standard defines malloc(0), why not fwrite(p, 0, n, stream)?
>

Like the man said `there are no semantics defined'.

Successfully writing `n' objects of zero size?  Just where will it all end?

I can see it now, `Programming in C+++':

    fp = fopen("/dev/null", "r+");
    p = malloc(0);
    fread(p, 0, 42, fp);
    p += 0;
    fwrite(p, 0, 42, fp);

Choose a better _algorithm_ and leave library routine breaking to the experts.


Boyd Roberts			boyd@necisa.ho.necisa.oz.au

``When the going gets wierd, the weird turn pro...''

gwyn@smoke.BRL.MIL (Doug Gwyn) (06/27/90)

In article <6818@scolex.sco.COM> seanf@sco.COM (Sean Fagan) writes:
>How many bytes get written for a 0-sized object?

There are no 0-sized objects.  Therefore the implementation may do what
it wants in this case.  I would suggest returning 0, which is not only
a technically correct answer (any nonnegative integer from 0 through
the number of records requested would be correct), but is also most
useful in detecting this misusage during software development.

karl@haddock.ima.isc.com (Karl Heuer) (06/28/90)

In article <6818@scolex.sco.COM> seanf@sco.COM (Sean Fagan) writes:
>In article <25105@mimsy.umd.edu> chris@mimsy.umd.edu (Chris Torek) writes:
>>making fwrite(, 0, n) return n is inexpensive
>
>But is it *right*?
>
>How many bytes get written for a 0-sized object?  What does ANSI say about
>it?  Do all of the committee members agree? 8-)

Since ANSI says there's no such thing as a zero-sized object, it would seem
that any behavior is right--including a core dump.  If the implementation
chooses to provide ZSOs as an extension, then the implementors should decide
on a reasonable behavior and document it.  If they want to be consistent with
other people providing similar extensions, then they should discuss it with
the X3J11 ZSO POC, which happens to be Doug Gwyn.

Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint