while I was reading: Where does the reference variable gets stored
about the way references get saved in the memory, I came to the accepted answer which says:
On the other hand, if the reference is "persistent" or visible to other translation units (such as a data member or a global variable), it has to occupy some space and be stored somewhere. In that case, it will most likely be represented as a pointer, and code using it will be compiled to dereference that pointer.
could someone elaborate this point? why the compiler can't simply treat all uses of r
as an alias for x[1]
, and access that int directly. (which is the case in the following function foo()
)
void foo()
{
int x[4] = {0, 1, 2, 3};
int &r = x[1];
// more code
}