F
Frederick Gotham
Let's assume that we're working on the following system:
CHAR_BIT == 8
sizeof( char* ) == 4 (i.e. 32-Bit)
Furthermore, lets assume that the memory addresses are distributed as
follows:
0x00000000 through 0xFFFFFFFE : Valid byte addresses
0xFFFFFFFF : Null pointer value
If we want to set a pointer to null, we can just do the following:
char *p = 0; /* Now contains 0xFFFFFFFF */
If, for some wacky reason, we wanted to set it to all bits zero, then we
could do:
memset(p, 0, sizeof p); /* Now contains 0x00000000 */
I'd like to know what happens however when we explicitly use a cast:
p = reinterpret_cast<char*>(0);
p = (char*)0;
Do the above two lines of code set our pointer to the legitimate null
pointer value, or do they set it to all bits zero? (My guess is that it's
the null pointer value)
Or consider the following:
#include <iostream>
int main()
{
unsigned i;
std::cin >> i;
/* Let's assume that user types in 0 */
char *p = reinterpret_cast<char*>( i );
}
I would presume that the above code snippet sets p to all bits zero in
the above, rather than the legitimate null pointer value.
I'm very familiar with the concept of "type mismatch"; for instance, I
realise that the following is broken:
double *p;
float k = 34.2;
p = k;
The literal, 0, is of the type "signed int". I've always thought it
strange that the following triggers a type-mismatch error:
char *p = 5;
While the following doesn't:
char *p = 0;
From the knowledge I have at the moment, it seems to me that an
expression known at compile-time to be equal to zero gets special
treatment -- even when cast explicitly. This would lead me to believe
that the following line sets p to the null pointer value:
char *p = reinterpret_cast<char*>( 7 - 5 - 2 );
, althought my example which contained user input would set the pointer
to all bits zero rather than the legitimate null pointer value.
CHAR_BIT == 8
sizeof( char* ) == 4 (i.e. 32-Bit)
Furthermore, lets assume that the memory addresses are distributed as
follows:
0x00000000 through 0xFFFFFFFE : Valid byte addresses
0xFFFFFFFF : Null pointer value
If we want to set a pointer to null, we can just do the following:
char *p = 0; /* Now contains 0xFFFFFFFF */
If, for some wacky reason, we wanted to set it to all bits zero, then we
could do:
memset(p, 0, sizeof p); /* Now contains 0x00000000 */
I'd like to know what happens however when we explicitly use a cast:
p = reinterpret_cast<char*>(0);
p = (char*)0;
Do the above two lines of code set our pointer to the legitimate null
pointer value, or do they set it to all bits zero? (My guess is that it's
the null pointer value)
Or consider the following:
#include <iostream>
int main()
{
unsigned i;
std::cin >> i;
/* Let's assume that user types in 0 */
char *p = reinterpret_cast<char*>( i );
}
I would presume that the above code snippet sets p to all bits zero in
the above, rather than the legitimate null pointer value.
I'm very familiar with the concept of "type mismatch"; for instance, I
realise that the following is broken:
double *p;
float k = 34.2;
p = k;
The literal, 0, is of the type "signed int". I've always thought it
strange that the following triggers a type-mismatch error:
char *p = 5;
While the following doesn't:
char *p = 0;
From the knowledge I have at the moment, it seems to me that an
expression known at compile-time to be equal to zero gets special
treatment -- even when cast explicitly. This would lead me to believe
that the following line sets p to the null pointer value:
char *p = reinterpret_cast<char*>( 7 - 5 - 2 );
, althought my example which contained user input would set the pointer
to all bits zero rather than the legitimate null pointer value.