[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
(void) casts when function return values are ignored
On Wed, 24 Aug 2011, Marc Balmer wrote:
A compiler can detect that I don't use the return value by
seeing that I don't use the return value.
Why tell the compiler that I don't want to use the return value?
It's a stupid rule, to say the least.
Of course the compiler can tell that you don't use the return
value. But neither the compiler nor a human reader can easily tell
that this is deliberate, as opposed to a mistake.
Functions with return values fall into a few broad categories:
1. Functions whose return value contains the result of the
function; e.g. sqrt(3).
2. Functions whose return value is used to report success or
failure; e.g. sscanf(3), write(2).
3. Functions whose return value contains information that is
uninteresting, or also available elsewhere; e.g. strcpy(3).
Failure to check the return value in cases 1 or 2 is almost always
a bug, but if you have a good reason to ignore the return value,
then the (void) convention is useful to tell both human readers
and code analysis tools that it's deliberate. (It might still be
a bug, but at least you thought a little about the fact that you
are ignoring the return value.)
Failure to check the return value in case 3 is perfectly normal,
and should not need any special annotation. However, some
imperfect code analysis tools complain if you don't have a (void)
cast, so some people are in the habit of using lots of (void)
casts to appease the tools.
I personally think that (void) casts in cases 1 and 2 are
useful, while (void) casts in case 3 are annoying and point to a
deficiency in the tools.
--apb (Alan Barrett)
Main Index |
Thread Index |