Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer typecasting with and without reference

Tags:

c++

casting

Given that A* pA; and B* pB;, is there ANY difference between below type castings (query for all C++ style casts):

pB = reinterpret_cast<B*>(pA);      // pointer
pB = reinterpret_cast<B*&>(pA);     // pointer-reference
like image 359
iammilind Avatar asked May 13 '11 08:05

iammilind


1 Answers

The two are radically different, at least in theory (and possibly on a few rare machines, in practice). The first takes a pointer to A, and converts it to a pointer to B; in theory, at least, this may involve changes in size and representation. (I've actually worked on machines where char* was larger than int*. I rather doubt that any such machines still exist, although perhaps in the embedded world...) The second is really the equivalent of *reinterpret_cast<B**>(&pA); it takes the bits in pA, and tells the compiler to interpret them as a B*. If the format is different, tough luck, and if the size is different, you're likely to only access part of pA or to access memory which isn't part of pA.

Also, the first is an rvalue, the second an lvalue. Thus, something like:

++ reinterpret_cast<B*>( pA );

is illegal, but:

++ reinterpret_cast<B*&>( pA );

isn't. This is a useful technique for obfuscating code, and getting unaligned pointers, pointers into the middle of objects, or other pointers you don't dare dereference.

In general, the second form should be avoided, but there are rare exceptions. Posix guarantees that all pointers, including pointers to functions (but not pointers to members—Posix specifies a C ABI, which doesn't have pointers to members), have the same size and format, so the second form is guaranteed to work. And it is the only way you can legally convert the void* returned by dlsym into a pointer to a function:

int (*pf)( int );
reinterpret_cast<void*&>( pf ) = dlsym( handle, "functionName" );

(In C, you'd write:

int (*pf)( int );
*(void**)( &pf ) = dlsym( handle, "functionName" );

, see the official specification.) Such tricks allow conversions between pointer types which aren't otherwise allowed, but depends on additional guarantees not in the standard.

like image 75
James Kanze Avatar answered Oct 11 '22 07:10

James Kanze