T
Tomás
Okay firstly I'll start with pointers. I have always thought of a pointer as
storage which holds a memory address, plain and simple. Let's say that on a
particular platform, an "int" is 32-Bit, and a memory address is 32-Bit.
Thus, if we have the following code:
int i = 7;
int* p = &i;
Then memory is allocated to store a 32-Bit "int", and the value "7" is
stored in it. Let's say that the address of this memory is 0x89ABCDEF. Now,
we store the address of "i" in the variable "p". So "p"'s value will be
0x89ABCDEF. They'll look like so in memory:
i: 0000 0000 0000 0000 0000 0000 0000 0111
p: 1000 1001 1010 1011 1100 1101 1110 1111
One of the first delicate matters about pointers is that a null pointer
isn't necessarily represented as all bits zero, as follows:
0000 0000 0000 0000 0000 0000 0000 0000
But, depending on the platform, it could have any bit pattern, such as:
1000 0000 0000 0000 0000 0000 0000 0000
or:
1111 1111 1111 1111 1111 1111 1111 1111
When you write the following in your code:
p = 0;
Then you're storing the null pointer value in a pointer variable, which may
or may not be the same bit pattern as integral zero.
Moving on...
Because of my understanding of a pointer as just storing a memory address, I
fail to understand why the following code wouldn't be legal:
int a;
int* p1 = &a;
char* p2 = reinterpret_cast<char*>( p1 );
double* p3 = reinterpret_cast<double*>( p2 );
std::string* p4 = reinterpret_cast<std::string*>( p3 );
int* p5 = reinterpret_cast<int*>(p4);
*p5 = 3;
I've often heard that the above code might not do what I expect it to do.
This leads me to believe that not all pointer types are the same. So my
first question is:
A) How are different pointer types different? My understanding is that they
simply store a memory address, so I would imagine they would all be the
same.
Another question which comes to mind is:
B) Do all pointer types have the same null value? Or can different types
have different null values?
Is there any sort of in-depth guide which deals with all the fanciful
possibilites of pointers?
And finally, is the following code legal:
int (&array)[5] = *reinterpret_cast< int (*)[5] > ( new int[5] );
delete &array[0];
If we look at it from the viewpoint of a pointer as simply storing a memory
address, then I forsee no problems with the above code.
-Tomás
storage which holds a memory address, plain and simple. Let's say that on a
particular platform, an "int" is 32-Bit, and a memory address is 32-Bit.
Thus, if we have the following code:
int i = 7;
int* p = &i;
Then memory is allocated to store a 32-Bit "int", and the value "7" is
stored in it. Let's say that the address of this memory is 0x89ABCDEF. Now,
we store the address of "i" in the variable "p". So "p"'s value will be
0x89ABCDEF. They'll look like so in memory:
i: 0000 0000 0000 0000 0000 0000 0000 0111
p: 1000 1001 1010 1011 1100 1101 1110 1111
One of the first delicate matters about pointers is that a null pointer
isn't necessarily represented as all bits zero, as follows:
0000 0000 0000 0000 0000 0000 0000 0000
But, depending on the platform, it could have any bit pattern, such as:
1000 0000 0000 0000 0000 0000 0000 0000
or:
1111 1111 1111 1111 1111 1111 1111 1111
When you write the following in your code:
p = 0;
Then you're storing the null pointer value in a pointer variable, which may
or may not be the same bit pattern as integral zero.
Moving on...
Because of my understanding of a pointer as just storing a memory address, I
fail to understand why the following code wouldn't be legal:
int a;
int* p1 = &a;
char* p2 = reinterpret_cast<char*>( p1 );
double* p3 = reinterpret_cast<double*>( p2 );
std::string* p4 = reinterpret_cast<std::string*>( p3 );
int* p5 = reinterpret_cast<int*>(p4);
*p5 = 3;
I've often heard that the above code might not do what I expect it to do.
This leads me to believe that not all pointer types are the same. So my
first question is:
A) How are different pointer types different? My understanding is that they
simply store a memory address, so I would imagine they would all be the
same.
Another question which comes to mind is:
B) Do all pointer types have the same null value? Or can different types
have different null values?
Is there any sort of in-depth guide which deals with all the fanciful
possibilites of pointers?
And finally, is the following code legal:
int (&array)[5] = *reinterpret_cast< int (*)[5] > ( new int[5] );
delete &array[0];
If we look at it from the viewpoint of a pointer as simply storing a memory
address, then I forsee no problems with the above code.
-Tomás