[comp.lang.c] Talking of void ...

jv@mhres.mh.nl (Johan Vromans) (10/24/87)

This problem occured when I ported a program to a UN*X system on which
signal handlers were "void (*func)()", instead of the usual "int (*func)()".
I used a "typedef int SHND_TYPE" in a system-dependent include file, 
which I changed to "typedef void SHND_TYPE". But then I ran in problems
with the return *action*, not the *value*:

SHND_TYPE catchit (...) {
	... some useful code ...
	return ((SNHD_TYPE) 0);
}

Maybe returning something which has been cast to void should be made
equivalent to "return;".

-- 
Johan Vromans                              | jv@mh.nl via European backbone
Multihouse N.V., Gouda, the Netherlands    | uucp: ..{?????!}mcvax!mh.nl!jv
"It is better to light a candle than to curse the da and 
 a

guy%gorodish@Sun.COM (Guy Harris) (10/25/87)

> This problem occured when I ported a program to a UN*X system on which
> signal handlers were "void (*func)()", instead of the usual "int (*func)()".

The "usual" type will become "void (*)()" in the future; that is the type
specified in ANSI C and thus in POSIX.  It is the type used by System V Release
3, and will be used in more systems as time goes on.  Had "void" been a part of
C all along, the type of a signal handler probably *would* have been "void
(*)()", since nobody actually *does* something with the return value of a
signal handler (and if somebody built a system where something *was* done with
that return value, all hell would break loose, since most people's signal
handlers don't return values).

> I used a "typedef int SHND_TYPE" in a system-dependent include file, 
> which I changed to "typedef void SHND_TYPE". But then I ran in problems
> with the return *action*, not the *value*:
> 
> SHND_TYPE catchit (...) {
> 	... some useful code ...
> 	return ((SNHD_TYPE) 0);
> }

Why are you returning a value here?  I know of no UNIX system where the return
value of a signal handler is used (and would doubt there *is* such a system,
for the reasons listed above).  Just put "return;" in place of the existing
return statement.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

jv@mhres.mh.nl (Johan Vromans) (10/27/87)

In article <31905@sun.uucp> guy%gorodish@Sun.COM (Guy Harris) writes:
>> 
>> SHND_TYPE catchit (...) {
>> 	... some useful code ...
>> 	return ((SNHD_TYPE) 0);
>> }
>
>Why are you returning a value here?

There are two reasons to return a value: one to keep lint from
complaining, and the other reason is a matter of programming principles. If
a function is typed, I always use an explicit return value. 



-- 
Johan Vromans                              | jv@mh.nl via European backbone
Multihouse N.V., Gouda, the Netherlands    | uucp: ..{?????!}mcvax!mh.nl!jv
"It is better to light a candle than to curse the darkness"

guy%gorodish@Sun.COM (Guy Harris) (10/27/87)

> There are two reasons to return a value: one to keep lint from
> complaining, and the other reason is a matter of programming principles. If
> a function is typed, I always use an explicit return value. 

Which is precisely why "void ()" is the correct type for a signal handler
function, since the value it returns isn't used....

The only reason why it was "int ()" is that "void" didn't exist as a C data
type when the C-language version of the UNIX signal mechanism first appeared.
As such, "int ()" had to serve a dual purpose; it was used both for functions
returning "int" and for functions returning nothing.  (This unfortunately meant
that the C compiler could not complain about functions declared (explicitly or
implicitly) as "int" that didn't return values.)  Signal handlers *really*
aren't "int ()"; their data type can be best described as "poor man's 'void
()'".  Now that "void" is supported in most C implementations, the data type
has been changed to "void ()".

Given that, and given the problems you have with moving signal handlers that
really return a value to modern systems with the proper type for signal
handlers, it would be better *not* to consider signal handlers really to be
"int ()", treat them as "void ()" (just not declaring them as such on older
systems), and don't have them return a value.

As for "lint", well, for the same reason the C compiler doesn't complain about
"int ()" functions that don't return values, "lint" doesn't complain about them
either.  It only complains if a function returns a value sometimes but not
other times, or if a function returns no value but somebody uses the
non-existent value (another good reason for using "void" - the *compiler*
can reject code that attempts to use the non-existent "value" of a "void ()"
function).

(The C++ compiler (the 1.1 version, anyway) *will* give a warning if you have a
function declared as "int" that doesn't return a value.)
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com