Code:
char keyStr[50]={ 0x5F, 0x80 /* bla bla */ };
uint32_t* reCast = reinterpret_cast< uint32_t* >( &keyStr[29] );
uint32_t* reCast2 = ( uint32_t* )
In what way do you mean "not dangerous"? reinterpret_cast is incredibly dangerous. It tells the compiler it is safe to ignore what it thinks it knows about the value.
It's not as dangerous as a c-style cast which throws away the const/volatileness of the value in question as well as any information about what it is pointing to.
Understanding these operations in assembly language is a bit pointless. They aren't assembly language constructs. They're C++ language constructs, that work something as follows:
static_cast
- Effectively this converts an object from one type to another. Note this can change the value (static_cast
doesn't have the same bit pattern as 1 for instance).
dynamic_cast
- if this object can be considered to be another type through inheritance, then treat it as such, otherwise render it as zero. This won't change the value of a pointer but it does safely change the compilers view of it.
const_cast
- throw away const
(or volatile
) qualifiers, which is not often a good idea as it allows you to destroy data the client thought was safe.
reinterpret_cast
- treat the bit pattern as meaning something different to what the compiler thought it did. Usually used for pointers and hopefully rarely. reinterpret_cast
ing an int into a float
is unlikely to be a good idea, but it will keep the same bit pattern.
c-style-cast - Take the bit pattern, forget completely what you know about it, and treat it as something else. A dangerous and almost invisible combination of static_cast
, reinterpret_cast
and const_cast
. It's not considered a good idea in C++ code because it's hard to spot in a review, and because it is not specific about what is happening.