Understanding reinterpret_cast?

I wrote the piece of code below just now with reinterpret_cast and then staic_cast(writing the same code for both) to try evaluate my understanding of the two and they both returned the same result which is not what I was expecting.

I guess my initial understanding before writing this was wrong, as I thought static_cast would give me a different return value to reinterpret_cast.

The result is 'e' to be precise about it. Which is the character literal for the binary value 101?

Is there someone who understands what my code is doing, that is willing to explain it, please?

1
2
3
4
5
6
7
8
9
10
  #include <iostream>
using namespace std;
int main()
{
   int x=101;
   int *xptr = &x;
   char y = reinterpret_cast<int>(*xptr);
   cout << y << endl;
   return 0;
}
closed account (E0p9LyTq)
reinterpret_cast will force the compiler to make a conversion, period. Even if it theoretically can't be done.

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
   int arr[5] = { 5, 10, 15, 20, 25 };

   int i = reinterpret_cast<int> (arr);

   std::cout << i << '\n';
}

10287228

(Your output may vary)

static_cast will change types when there are known and user-created conversions available.

Change reinterpret_cast above to static_cast and the compiler balks.

reinterpret_cast is the C++ equivalent to C style casting, (some type) aVariable. It can cause major problems.

https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used
Last edited on
10287228? What value has been placed into 'i'? As they are both an int type, is it the memory value of arr[5] how it would be represented as a single interger
closed account (E0p9LyTq)
Why?

An array stores the elements in contiguous memory blocks. The compiler treats each blocks as a separate entity.

reinterpret_cast forces the compiler to see the 5 separate blocks as one block five times bigger.

I missed it the first time, your use of reinterpret_cast is logically flawed. You thought you were casting int to char. You actually cast int to int. The compiler can do an implicit cast from int to char itself.

Line 7 is the equivalent of char y = (*xptr);.

char y = reinterpret_cast<char>(*xptr); is an invalid cast. char y = reinterpret_cast<char>(xptr); is a valid cast, if imprecise due to bit truncation.
Last edited on
reinterpret_cast and static_cast each do a very specific set of conversions. http://en.cppreference.com/w/cpp/language/static_cast and http://en.cppreference.com/w/cpp/language/reinterpret_cast have the detailed lists, but it's basically just these four things for reinterpret_cast:

* retyping references
* retyping pointers
* obtaining an address out of a pointer (or building a pointer from an address)
* same-type conversion (which does nothing)

1
2
3
   int x=101;
   int *xptr = &x;
   char y = reinterpret_cast<int>(*xptr);

here *xptr is an int, the cast to int is a same-type conversion that does nothing. This is equivalent to char y = 101;

I see, I didn't spot that.

I will take all of this on board and take something from it. This helped a lot I think, thanks.
Topic archived. No new replies allowed.