Article View: comp.std.c
Article #3121Re: gcc and NULL function pointers.
From: Peter Holzer
Date: Mon, 10 Jun 1991 12:57
Date: Mon, 10 Jun 1991 12:57
48 lines
2322 bytes
2322 bytes
sef@kithrup.COM (Sean Eric Fagan) writes:
>ANSI does not require a prototype for non-variardic functions (as far as I
>can tell). As a result, for a certain class of popular machines, under
>certain compiler options, defining NULL as 0 will be incorrect, while
>'(void*)0' is correct.
>And the program that would core-dump on this would be perfectly correct,
>according to ANSI. The header file would, therefore, be wrong.
No.
Consider the following program on a machine where the representation of
char/void * and int * is different:
-----------------------------------------------------------------------
#define NULL ((void *)0)
int printf (char const * fmt, ...);
int main ()
{
a (NULL);
return 0;
}
int a (ip)
int * ip;
{
if (ip != NULL) printf ("%d\n", * ip);
}
-----------------------------------------------------------------------
The function expects a pointer to int, but gets a pointer-to-void. Thus
the contents of ip are undefined and it may well compare != NULL,
causing a core dump (or crash, or rude mail to your boss :-) on the
printf.
Moral: If no prototypes are in scope you always have to cast NULL to
the correct pointer type. So it does not matter if NULL is defined as 0
or (void *)0. Of course (void *)0 may save many sloppy programmers, but
it can cause problems if you want to cast NULL to a function pointer
(as the original poster mentioned).
--
| _ | Peter J. Holzer | Think of it |
| |_|_) | Technical University Vienna | as evolution |
| | | | Dept. for Real-Time Systems | in action! |
| __/ | hp@vmars.tuwien.ac.at | Tony Rand |
Technical Details
Message-ID:
<1991Jun10.125740.8102@email.tuwien.ac.at>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!uunet!email!vmars!hpReferences:
<4641@inews.intel.com> <1991Jun10.061202.25199@kithrup.COM> <1991Jun10.073125.25120@tkou02.enet.dec.com> <1991Jun10.081924.26439@kithrup.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!uunet!email!vmars!hp From: hp@vmars.tuwien.ac.at (Peter Holzer) Newsgroups: comp.std.c Subject: Re: gcc and NULL function pointers. Message-ID: <1991Jun10.125740.8102@email.tuwien.ac.at> Date: 10 Jun 91 12:57:40 GMT References: <4641@inews.intel.com> <1991Jun10.061202.25199@kithrup.COM> <1991Jun10.073125.25120@tkou02.enet.dec.com> <1991Jun10.081924.26439@kithrup.COM> Sender: news@email.tuwien.ac.at Organization: Technical University Vienna, Dept. for Realtime Systems, AUSTRIA Lines: 48 Nntp-Posting-Host: nowhere.vmars.tuwien.ac.at