C++ Pointers vs References – Low-Level Differences

assemblyc++pointersreference

If we have this code:

int foo=100;
int& reference = foo;
int* pointer = &reference;

There's no actual binary difference in the reference's data and the pointer's data. (they both contain the location in memory of foo)

part 2

So where do all the other differences between pointers and references (discussed here) come in? Does the compiler enforce them or are they actually different types of variables on the assemebly level? In other words, do the following produce the same assembly language?

foo=100;
int& reference=foo;
reference=5;

foo=100;
int* pointer=&foo;
*pointer=5;

Best Answer

Theoretically, they could be implemented in different ways.

In practice, every compiler I've seen compiles pointers and references to the same machine code. The distinction is entirely at the language level.

But, like cdiggins says, you shouldn't depend on that generalization until you've verified it's true for your compiler and platform.