C++ reinterpret cast

reinterpret_cast in C++

One of the most dangerous and notorious type of casting in the entire C++.Unlike the other method reinterpret_cast treat data as just a bit pattern that can be casted from one type to another type.It conveniently cast from one pointer type to another type without caring for what follows.Consider the code below.

char c=’C’ , *cpt=&c
int *i=reinterpret_cast< int* > (cpt) ; ///char* casted to int*

cout<< *i ; ///undefine value

Accessing the value using *i will give you an undefined value,but why?.One of the reasons is char* points to char value which is just 1 byte large and the int* pointer is meant to point to int type which should be 4 bytes large.So when cpt is casted to int* type ‘i’ still needs to point to 4 bytes memory.But cpt points to only 1 byte memory.So the question is,where is the extra 3 bytes going to come from? It is simple the compiler had to just take 3 bytes from somewhere and bam! put it with the 1 bytes of the original character value to make it a total of 4 bytes.This sure is not a reasonable act on the part of the compiler but don’t blame it because reinterpret_cast had force the compiler to do it.So,blame the reinterpret_cast instead.Well this is one of the troubles created by reinterpret_cast.

That being said it’s not like there is no solution for this,in fact there is and a simple one.Before we use any pointer which the reinterpret_cast result is assigned to we will cast it back to it’s original type.After this we can safely use the pointer to access the value or do anything.

cout<< *(reinterpret_cast< char* >(i) );

Now it outputs


which is the original value pointed by cpt.

General discussion on reinterpret_cast

reinterpret_cast works only on pointers and reference.It does not work on variables .So the basic function of reinterpret_cast is to cast from one pointer to another type.It can also cast a void pointer to any type which is another notorious act of the reinterpret_cast;static_cast also exhibit this behavior.

int i=908;
float f=reinterpret_cast<int>(i) ;///error

void *vd=&i ;
float *fpt=reinterpret_cast<float*>(vd);

cout<< *fpt ; ///undefine output

Here again we must cast back fpt to int type to access the original value.

int* ipt=reinterpret_cast<int>(fpt) ;

cout<< *ipt ; ///gives 908


Stop wasting time,earn money($$$) from your website-Join Now!

structure and class pointer

Another notorious act of reinterpret_cast is it can cast a pointer of one structure to another structure.This certainly doesn’t make any sense. Consider the code below.

struct A { } ;

struct B{ } ;

A a ,*apt=&a ;

B *b=reinterpret_cast<B>(apt) ;

Does it make sense to you here casting ‘a’ to B type,I don’t think so.

in-depth discussion on reinterpret_cast

As stated before reinterpret_cast treat any pointer object as just a raw bit pattern ,it is not concerned about the type the bits represent on the surface.All it does is interpret the raw bits of one type to another type.To show you how reinterpret_cast interpret the raw bits pattern consider the example below.

int i=890 ,*ipt=&i ;

char *cpt=reinterpret_cast<char*> (ipt) ;

cout<< *cpt ;

The output is


why? let’s look at the raw bits of 890 which is,
reinterpret_cast in-depth discussion

Now reinterpret_cast will try to interpret this binary digits to char type.Since char type size is only 1 byte so out of the 4 bytes only 1 bytes-the 8 left most bits-is considered which is 01111010.This binary digits is same as the binary digits of the value 122.Since the new type is char it will try to interpret this binary digit in character.If we look at the ASCII chart the character corresponding to the value 122 is ‘C’.So ‘C’ is printed as the output value.This also means the remaining 3 bytes is neglected when reinterpret_cast is performed,but note here the 3 bytes is still part of the variable ‘i’ so it is still a valid memory. So you see reinterpret_cast simply takes the underlying bits and cast it to another type.

Related Link

->C++ static_cast

->C++ const_cast