NULL etc.

Paul_L_Schauble at cup.portal.com Paul_L_Schauble at cup.portal.com
Wed Jun 29 18:00:04 AEST 1988


I don't mean to extend the continuing discussion about the value of NULL, but
i have to ask:

  is #define NULL (char *)0 really portable??

I'm assuming a somewhat odd machine for sake of argument. Consider the
common sequence
1:    long *ptr_to_long;
2:    ptr_to_long = 0;
3:    if (ptr_to_long == NULL) .....

I can visualize a machine where pointer to long (word aligned) and pointer
to char have different formats and for the null pointers in each type to
have different non-zero values. 

Clearly the compiler is obligated to convert to the appropriate null
pointer value in line 2. Expanding the macro and the cast in line 3 this 
becomes

1a:	long *ptr_to_long;
2a:	char *temp;
3a:	ptr_to_long = 0;
4a:	temp = 0;
5b:	if (ptr_to_long == temp) xxxx

Again, the compiler is obligated to convert to the appropriate null value
in 3a and 4a. But not in 5b. So they may well compare unequal. (I think.)

According to K&R, the comparison in 5b is undefined. Pointer comparisons
are only defined between pointer of the same type that point at the same
array. 

This becomes a very interesting question since I have seen postings that
state that the value on NULL for ANSI C is (void *)0. Seems to me that this
has all of the same problems. Is this really what the standard says? If so,
where have I gone wrong above?

    Paul
  implemented, although the space station looks like it's going to be
another bare-bones minimum budget thing with all the emphasis on keeping the
up-front cost low, regardless of operational cost.  Sigh.
     But that's the province of the politicians and beaurocrats.
    It's also hard to believe just how conservative the actual spacecraft
manufacturers are.  Nobody wants to fly anything that hasn't already been
demonstrated in space already.  This is very frustrating to those people who
want to advance the technology--but nobody wants to risk a few million dollars
on something new that might work a tiny bit better if there's any chance at
all that something unfor



More information about the Comp.lang.c mailing list