Different behavior between ref and pointer


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?


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.


printf("%x %x\n", u, v);

for me prints:

39fdb8 39fdb8

Not garbage, but the value of v.


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; }


  • Cannot use std::iota with std::set
  • ES6 class and class instance as parameter in constructor
  • gcc ld: overlapping sections tbss, .init_array in statically-linked ELF binary
  • OpenCV detect square with difficult background
  • Last revision Chromium Embedded Framework 3
  • Bit not operation in PHP(or any other language probably)
  • VBA: Extract Top 'x' Entries from each category
  • Can I use jquery to blank textarea fields or ajax like input boxes?
  • Help translating Reflector deconstruction into compilable code
  • Using : for multiple slicing in list or numpy array
  • Android: How to open the webview in a new screen
  • Get all existing pointers to an object
  • Java Garbage collection, setting reference to null
  • Excel distinct count with conditions
  • Where these are stored?
  • Examples of how to a STS in .Net 4.5 using WCF
  • How to display special characters in SQL server 2008?
  • Memory error in python- how to use more memory
  • android google indoor map
  • Multicolored edittext hint
  • Differences in dis-assembled C code of GCC and Borland?
  • Redux Form - Not able to type anything in input
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • How can I sort a a table with VBA with given text condition?
  • Meteor: Do Something On Email Verification Confirmation
  • Cannot resolve symbol 'MyApi'
  • Java: can you cast Class into a specific interface?
  • Algorithm for a smudge tool?
  • All Classes Conforming to Protocol Inherit Default Implementation
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • using HTMLImports.whenReady not working in chrome
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Bitwise OR returns boolean when one of operands is nil
  • Checking variable from a different class in C#