I wonder if using a pointer-to-a-function
with NULL is ok?
Given that a pointer-to-void may only point to
objects, and NULL may be defined as ((void*)0), and
pointers to objects are not compatible with pointers
to functions?
I have always been initializing function-pointers
with NULL, and testing if they are NULL etc.,
but now I think I'd better use 0 instead.
Or is that paranoid and NULL is ok in this context?
Greetings,
Bernd
--
comp.lang.c.moderated - cl...@plethora.net
Using NULL in this way is perfectly conforming, as it is
expressly allowed by the standard in 6.2.2.3:
===================
An integral constant expression with the value 0, or such an
expression cast to type void *, is called a null pointer
constant. If a null pointer constant is assigned to or compared
for equality to a pointer, the constant is converted to a
pointer of that type. Such a pointer, called a null pointer, is
guaranteed to compare unequal to a pointer to any object or
function.
Two null pointers, converted through possibly different
sequences of casts to pointer types, shall compare equal.
====================
Thus, while pointers to objects and functions cannot be used
interchangeably, they can all be set to NULL, tested for NULL,
and a null pointer to a function will compare equal to a null
pointer to any object.
Jack
--
comp.lang.c.moderated - cl...@plethora.net
Bernd Luevelsmeyer <bernd.lue...@iplan.heitec.net> wrote in article
<clcm-1997...@plethora.net>...
> Hi,
>
> I wonder if using a pointer-to-a-function
> with NULL is ok?
>
Yep.
> Given that a pointer-to-void may only point to
> objects, and NULL may be defined as ((void*)0), and
> pointers to objects are not compatible with pointers
> to functions?
>
> I have always been initializing function-pointers
> with NULL, and testing if they are NULL etc.,
> but now I think I'd better use 0 instead.
> Or is that paranoid and NULL is ok in this context?
I believe the original issue here was that 0 (zero) interpreted as an int
was on many systems a 16 bit quantity. The Macro #define NULL ((void*)0) is
guaranteed to generate the right size 'zero'.
I don't know of any C implementation that allows code to reside at 0000
(doesn't mean there isn't one of course)
I had never had a problem using function pointers that are tested against
NULL (presumed to mean no action to be taken)
Mitch.
--
comp.lang.c.moderated - cl...@plethora.net
No; a null pointer to a function can be converted to a null pointer
to an object or incomplete type via an explicit cast and then
it will compare equal to another null pointer of that type. :)
If you do a straight comparison between a function pointer and object
pointer, you trigger a constraint violation.
--
comp.lang.c.moderated - cl...@plethora.net
Accordingly to some sources you can append 'L' to '0',
like '0L' which makes zero to be interpreted as a 32-bit.
BTW, are you sure void* is 32bit on DOS? At the moment,
I don't have an access to DOS to check it.
--
Vlad Petersen | <vladimip at uniserve dot com>
#include <disclaimer.h> | *Good pings come in small packets*
Vancouver, BC | Windows: for IQs smaller than 95
SIGSIG -- signature too long (core dumped)
--
comp.lang.c.moderated - cl...@plethora.net
I normally use a boolean expression:
void (*func)();
func = NULL;
/* .... */
if (func) func();
/* .... */
I think that's pretty safe. If you want to get paranoid you can define
something like this:
typedef void (*PFV)(); /* pointer to function returning void */
#define NULL_FUNC (PFV)NULL;
PFV func;
func = NULL_FUNC;
/* .... */
if (func!=NULL_FUNC) func();
BTW, I vaguely recall from my dim dark past that executing a NULL
function produces a safe no-op.
e.g.
void (*func)();
func = NULL;
func();
Can anyone confirm/deny that?
--
########################################################################
Michael Smith emme...@zip.com.au
Emmenjay Consulting Pty Ltd http://www.zip.com.au/~emmenjay/
Computers *ARE* user-friendly. You just need to be properly introduced.
--
comp.lang.c.moderated - cl...@plethora.net
> Mitch wrote:
> > I believe the original issue here was that 0 (zero) interpreted as an int
> > was on many systems a 16 bit quantity. The Macro #define NULL ((void*)0) is
> > guaranteed to generate the right size 'zero'.
>
> Accordingly to some sources you can append 'L' to '0',
> like '0L' which makes zero to be interpreted as a 32-bit.
>
> BTW, are you sure void* is 32bit on DOS? At the moment,
> I don't have an access to DOS to check it.
-Eh?- 0 is garunteed to always convert correctly to a null pointer. The
size of 0 or 0L or of a 'void *' is irrelevant.
[- firewind -]
[- email: fire...@metroid.dyn.ml.org (home), fire...@aurdev.com (work) -]
[- "You're just jealous because the voices talk to -me-." -]
[- Have a good day, and enjoy your C. -]
[- (on a crusade of grumpiness where grumpiness is due) -]
--
comp.lang.c.moderated - cl...@plethora.net
Hi,
Thanks for all the answers.
To summarize, NULL with function-pointers is ok
because there's a special dispensation for NULL-pointer-constants
so "(void*)0" may be used although, generally, void-pointers
may not point to functions.
And, according to my test, I can deny that calling a function-pointer
that is set to NULL is no-op. It gives a crash, and IMHO it should,
because a NULL-pointer is dereferenced.
Thanks again,