Perhaps I'm confused, but I thought one of the advantages of using
NULL or 0 in a comparison was that the compiler would do whatever was
necessary to make it work properly. Or am I only semi-confused, that
is, does that only apply to data and data pointers?
--
"'If God foreknew that this would happen,
it will happen.'"
> Perhaps I'm confused, but I thought one of the advantages of using
> NULL or 0 in a comparison was that the compiler would do whatever was
> necessary to make it work properly.
That advantage applies completely only in the case of using an integer
zero. NULL (particularly in C compilers) has a strong tendency of being
defined as ((void *)0), which makes it a data pointer.
The case where that makes a difference is when you use NULL in
connection to function pointers. Function pointers and data pointers
don't mix. In a nutshell, C assumes a Harvard architecture, and that's
a fact surprisingly many, even experienced C programmers aren't aware of
at all.
Yes, it is valid from the point of view of segmentation.
> If so, then comparing offsets won't work -- even in Flat model. But
> perhaps I am wrong and the first byte or bytes are not valid targets
> for the offset. Perhaps the old DOS (or was that originally CP/M?)
> prefix lives on, even in 32-bit protected mode, even in Flat model!
Flat memory model uses paging as the primary protection scheme.
Most implementations make the first pages (and sometimes more) inaccessible.
This is to catch null-pointers. OWs CLIB also has a similar feature, but
places
an INT 3 first in the executable for small-code memory models just to catch
invalid calls.
The problem here is that in compact memory model, all function pointers
are near, and assumed to be within a single code-segment. The function null
pointer is offset 0. That is probably why OW extends the function pointer to
48-bits by adding current CS. This would be perfectly valid if comparing
near
pointers with far pointers, but it is not valid when it is compared to a
48-bit
null pointer, as this comparison is always not-equal, and useless.
Leif Ekblad