tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: NULL pointer arithmetic issues



> Oh.  And I actually do not believe it has to be a constant.

You are correct; it does not need to be a simple constant.

> The text says "integer constant expression with the value 0, or such
> an expression..."

Yes.  (void *)(1-1) is a valid null pointer constant.  So, on an
all-ASCII system, is (0x1f+(3*5)-'.').  But, in the presence of

int one(void) { return(1); }

then (one()-one()) is not - it is an integer expression with value
zero, but it is not an integer _constant_ expression.  It's entirely
possible that (int *)(one()-one()) will produce a different pointer
from (int *)(1-1) - the latter is a null pointer; the former might or
might not be, depending on the implementation.  Similarly,

int i;

 i = 0;
 if ((int *)i == (int *)0) ... else ...

may test unequal.  (I have a very fuzzy memory that says POSIX may
impose additional restrictions that might affect this; I'm talking
strictly about C99 here.)

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index