Need some examples

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Wed Jul 31 07:02:26 AEST 1985


>     There are a variety of funny little problems with using the 0
> pointer in C.

There is no "the" 0 pointer.  There are an infinite variety of null
pointers, and only in certain contexts (e.g., comparison against 0
integer constant) is one permitted to pretend otherwise.  This
subject is discussed in this newsgroup every few months.

> 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).

"Ignorance is no excuse."

> I suspect the argument is wrong (and dangerous), but don't have
> examples of machines on which this trick fails.  Anybody got
> ammunition out there to help my side out?

Any "trick" whose working is not guaranteed by the language
definition should not be relied upon!  Your organization seems to
have a real problem with people giving bogus advice.  Why in the
world would anyone WANT to formalize such a "trick"?

You should not need an actual implementation example.  I will give
a hypothetic example, though.  Perhaps some real implementation is
like this, perhaps not; the fact that it is permitted should rule
out using the "trick", which will not work in this case.  There are
also other possible ways that the "trick" could fail, including the
use of tagged architectures, non-zero bit patterns for null
pointers, and so on.

EXAMPLE:

Consider a 16-bit word-addressed architecture (e.g., CDC 1700,
H-316, perhaps DG Nova).  It is possible to represent any (int *)
in a single word, but it takes 2 words to represent a (char *).
Consider the function
	void foo( int a, int *b, int c );
Suppose that for economy's sake (very important on 16-bit machines)
the stack size of a function parameter is the minimum number of
words needed to hold it (1 word for each of the 3 parameters in this
example).  Now, suppose one invokes the function as
	foo( 1, (char *)0, 3 );
Because of the parameter layout, either the first or the third
parameter will not be transmitted correctly (it will have part
of the (char *) in its place on the stack frame).  QED.



More information about the Comp.lang.c mailing list