passsing a zero instead of a pointer
Marc Mengel
aaz at pucc-i
Tue Aug 6 11:47:25 AEST 1985
Followup-To: net.lang.c
Path: ...!pur-ee!pucc-i!aaz
Keywords:
Summary: passing a 0 for a pointer argument is broken
> f(ptr)
> foo *ptr;
> {
> [code]
> }
>
> calling f(0) is dangerous. But my friend contends that calling
> f() with a zero char pointer, e.g. f((char *)0), is safe, because
> (char *) is the largest pointer, and that he doesn't know of a C
> implementation that doesn't pass all pointers as the largest
> possible pointer (ala the way floats always get passed as doubles).
The only *correct* thing to pass to f() is a "(foo *) 0". The problem here
is that if you call f with a zero (which is an int) and the function is
expecting a pointer (which *could* need to be as big as a long int),
which gives either an upper or lower half of a long int which is GARBAGE on
some C compilers (like most for the 68000 which use 16 bit ints, and 32
bit long ints and pointers ).
The (char *) should work on most machines, but once again, it is
possible that there may be different sized or alligned pointers for
various objects. (Does anybody remember the discussion some months ago
about encodeing the size of what a pointer points to in the upper n
bits and shifting it over before using it?...) So I emphasise, the only
Right Thing to do here is to pass a (foo *) 0. Besides, lint even likes
it :-)
--
Marc Mengel
{ihnp4|decvax|icalqa|uiucdcs|cbosgd|harpo}!pur-ee!pucc-i!aaz
...!purdue!pucc-i!aaz
(Message inbox:30)
Return-Path: <agt at pucc-k>
Received: from pucc-k (pucc-k.ARPA) by pucc-i; Tue, 30 Jul 85 17:09:51 est
Received: by pucc-k; Tue, 30 Jul 85 17:07:57 est
Date: Tue, 30 Jul 85 17:07:57 est
From: Kevin Braunsdorf <agt at pucc-k>
Message-Id: <8507302207.AA06122 at pucc-k>
Uucp: { decvax, icalqa, ihnp4, inuxc, sequent, uiucdcs }!pur-ee!pur-phy!csc
{ decwrl, hplabs, icase, psuvax1, siemens, ucbvax }!purdue!pur-phy!csc
Apparently-To: aaz
More information about the Comp.lang.c
mailing list