expr?(void):(void)
Ray Butterworth
rbutterworth at watmath.UUCP
Sat Aug 2 04:10:26 AEST 1986
> In article <5826 at think.COM>, sam at think.COM (Sam Kendall) writes:
> > void PutC(Stream, char);
> > But for efficiency you want to write PutC as a macro. So you define it
> > like this:
> > extern void _WriteBuf(Stream);
> > #define PutC(s, c) (--(s)->count >= 0 ? (void) (*(s)->bufp++ = (c)) \
> > : _WriteBuf(s, c))
> I won't argue with the desirability of being able to do this, but
> I believe your example is poorly chosen. I/O functions can always fail
> and some indication of this ought to be returned to the user where
> possible, even though few bother to check usage like this.
> -- Jon Leech (...seismo!amdahl!jon)
But you don't know what _WriteBuf() does (neither do I). But we
are working on a stdio-like library at waterloo which looks very
similar to this. Checking the value returned by putchar() every
single time can be quite a waste, especially when you consider
that the only time a bad value can be returned is when the buffer
is flushed, and that only happens every 8000 characters or so
using full buffering on stdout. It is also a pain to have to
write the code to do this check and to write whatever code is
required to handle the problem (usually print a message and exit),
especially if putchar is used in many different places in the code.
That is why "few bother to check usage like this".
The solution we use is to have the _WriteBuf function, instead of
returning an indication of error, either print the message and
exit itself, or "raise an event" that can be trapped in the user's
code (signals or longjumps). That way users never need to check
the return status since the io function can never fail (or if it
does it doesn't return). It makes writing correct code much simpler,
and makes previously incorrect code correct. It also gets rid of
those thousands of useless status checks while adding no extra
overhead. Of course this method isn't necessarily the best to use
in all cases, only in almost all cases.
More information about the Comp.lang.c
mailing list