Re: conversion from int to int * of zero in comma operator
Frederick Gotham wrote:
kanze posted:
www.fruitfruit.com wrote:
The following is fine.
int* p = reinterpret_cast<int*>(0, 0);
But may give different results than:
int* p = 0 ;
Just to express that pedantically:
The first one:
int *p = reinterpret_cast<int*>(0,0);
sets the pointer to all-bits-zero, and is equivalent to:
memset(&p, 0, sizeof p);
No. It maps the integer value 0 to a pointer, using an
implementation defined mapping. The intent is that it "be
unsurprising to those who know the addressing structure of the
underlying machine," but that's just intent. And who knows what
could be considered "unsurprising" on some strange architecture.
(Or even in certain cases on what was in the past a very common
architecture---on a PDP-10, I would definitly not expect all
zero bits for "reinterpret_cast<char*>(0,0)". The hardware
supported bytes of many different sizes, and the size of the
byte was encoded in the pointer.)
The second one:
int *p = 0;
sets the pointer to its null pointer value (which may or may
not be all- bits-zero).
And may or may not be the same as mapping an integer value 0 to
a pointer.
Note that the following two also have the same effect of
setting the pointer to its null value:
int *p = (int*)0;
int *p = reinterpret_cast<int*>(0);
Because of the rule that conversion of a null pointer constant
to a pointer always results in the null pointer. Note, however,
that:
assert( (int)(int*)0 == 0 ) ;
is not guaranteed either (but the reverse is: if you convert a
pointer to a sufficiently large integral type, and back, you are
guaranteed to get a pointer which compares equal to the original
pointer).
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]