wtf is reinterrpret_cast

Programming, for all ages and all languages.
Post Reply
B.E

wtf is reinterrpret_cast

Post by B.E »

today I had a lecture on c++. I am confused with the diferrence between a stardard cast and a reinterrpret_cast.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:wtf is reinterrpret_cast

Post by Solar »

C style cast:

Code: Select all

(mytype_t)foo;
The problem with that is that it casts foo to mytype_t, period. Several things can go wrong here, and the compiler can't really complain because it has been told it should do this - the code might be syntactically correct, but all kinds of bugs can be hidden by a bad cast. And you can't even grep for it efficiently...

C++ introduces four different types of cast:
  • const_cast - add or remove "const" qualifier
  • dynamic_cast - casting class pointers up or down the inheritance graph, including proper type-checking
  • static_cast - like dynamic cast, but without type checking
  • reinterpret_cast - cast between pointers of unrelated classes
The advantage is that the compiler has some idea of what you're trying to do with the cast, and can tell you if you're doing it wrongly. It's also easy to grep for them.

reinterpret_cast, to answer your question, takes a pointer to something (let's say, a void *), and tells the compiler to handle the memory pointed to as something completely different, let's say a mytype_t *.

Needless to say that, if the memory pointed to is something else but a mytype_t, you'll be in trouble shortly.

Effectively, reinterpret_cast is equivalent to C-style casting, and you should avoid it if at all possible.

Edit: http://www.cplusplus.com/doc/tutorial/typecasting.html
Every good solution is obvious once you've found it.
B.E

Re:wtf is reinterrpret_cast

Post by B.E »

it makes sence now thanks solar
ark

Re:wtf is reinterrpret_cast

Post by ark »

The information above is essentially correct, but I don't think I'd describe reinterpret_cast as "C-style casting". C-style casting is simply casting with the type name in parentheses, and in fact one of the problems with it is that it does all of the C++ casts except dynamic_cast.

Of particular note is that doing a static_cast from void* to mytype_t* isn't really any different than doing a reinterpret_cast between the two. Where the difference lies is that reinterpret_cast allows you to cast directly from something like a double* to an int* -- i.e., pointers between unrelated types. You can achieve the same result with static_cast, but you first have to cast to void* and then cast to the unrelated type. So:

Code: Select all

double d = 2523.2342;

// the following will give an error

int* p1 = static_cast<int*>(&d);

// the following two lines effectivey do the same thing

int* p2 = static_cast<int*>(static_cast<void*>(&d));
int* p3 = reinterpret_cast<int*>(&d);
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:wtf is reinterrpret_cast

Post by Colonel Kernel »

Also, C-style casts can sometimes mean reinterpret_cast when you want them to mean static_cast. I found a bug once where a C-style downcast in the presence of multiple inheritence produced an invalid pointer.

Bottom line: Avoid C-style casts in C++ as a matter of habit.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:wtf is reinterrpret_cast

Post by Solar »

Joel wrote: The information above is essentially correct, but I don't think I'd describe reinterpret_cast as "C-style casting".
In the meaning of, does it no matter whether it makes sense or not. ;)
Every good solution is obvious once you've found it.
Post Reply