0

This is just an oddity I ran into and can't quite understand what's happening.

int main()
{
    int i{ 5 };

    void* v = &i;

    int* t = reinterpret_cast<int*>(v);
    int u = reinterpret_cast<int&>(v);

    int i2 = *t;

}

t is correctly 5 and u is garbage.

What's going on?

Tavison
  • 1,523
  • 1
  • 11
  • 19

3 Answers3

3

v stores the address of i, which is, as you put it, garbage. (it's not really garbage, but the value itself is meaningless, except as it is the address of i in this particular run of the program). u stores the same value (bitwise) as v, but reinterpreted as an integer, so it is "garbage".

t is not 5, as you claim. Or if it is, it's an extremely unlikely coincidence. t is the address of i, and i is 5, so *t (that is, t dereferenced) is 5.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • What I meant was the debugger resolved the address, so I did indead see 5 as the pointed to value. – Tavison Jan 17 '14 at 04:22
0
printf("%x %x\n", u, v);

for me prints:

39fdb8 39fdb8

Not garbage, but the value of v.

BWG
  • 2,238
  • 1
  • 20
  • 32
0

u is int v is pointer *v is int, but we should the following code can output 5 5

#include <iostream>
using namespace std;
int main()
{
    int i{ 5 };

    void* v = &i;

    int* t = reinterpret_cast<int*>(v);
    int u = reinterpret_cast<int&>(*(int*)v);

    int i2 = *t;

    std::cout << *t << std::endl;
    std::cout << u << std::endl;
    return 0;
}
michaeltang
  • 2,850
  • 15
  • 18
  • No need to mix c++ and c styles of casting. In this case it's simple enough: `int u = reinterpret_cast(*v);` – vershov Jan 17 '14 at 04:18
  • @vershov No so simple I'm afraid, `void` is an incomplete type – Praetorian Jan 17 '14 at 04:20
  • @Praetorian reinterpret_cast is a compiler directive which instructs the compiler to treat the sequence of bits of expression as if it had the type . – vershov Jan 17 '14 at 04:35
  • 1
    @vershov Fascinating, but how do you dereference a `void *`? – Praetorian Jan 17 '14 at 04:37
  • @Praetorian `void* v` is a pointer that is just an address to the memory. `*v` is sequence of bits located at the place where the pointer points to. Once we pass sequence of bits to the `reinterpret_cast` we don't need anything else to build new type, just this bit's sequence. – vershov Jan 17 '14 at 04:52
  • @vershov I apologize, it seems you're right. It is indeed just the starting location that matters in this context. Can't say I've ever tried to `reinterpret_cast` a `void*` to an `int&` before, so thank you for teaching me something new. Edit: Or maybe not, both VS2013 and gcc4.8 [agree with my earlier assertion](http://coliru.stacked-crooked.com/a/3ddd4dba142009e0), but clang compiles and outputs the correct answer. – Praetorian Jan 17 '14 at 05:09
  • @vershov I posted a [question](http://stackoverflow.com/questions/21178903/reinterpret-cast-behavior-when-dereferencing-a-void-pointer), let's see if someone can shed light on the difference in behavior – Praetorian Jan 17 '14 at 06:00