Let us say I have the following code:
Foo *foo = something();
uintptr_t bar = reinterpret_cast<uintptr_t>(foo);
Does bar
represent the address of foo
or the address that foo
points to? I believe it's the address of foo
, but I want to be certain that I am not mistaken.
Some clarification
What is uintptr_t data type explains that
In C99, it is defined as "an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer".
But this doesn't state what is being stored. Could the compiler conceivably store the address that foo
points to in bar
instead and then construct a new pointer with a different address when casting back to Foo
from uintptr_t
? Perhaps I'm misunderstanding pointer comparisons, but since they point to the same object, will they not compare equal?
I guess I'm looking for some pedantic clarity about about if I add the following line:
Foo *foo2 = reinterpret_cast<Foo *>(bar);
What on earth happens here? Undefined behavior? foo2
just points to the same object as foo
?