I have read that reference variable shares the same memory address with the original variable but also takes up some space on the stack. And as reference has the same memory address as the original variable, it is also known as alias.
So, my question is how memory allocations are done for reference variables ?
8.3.2 References §4
It is unspecified whether or not a reference requires storage
That being said, if a reference needs storage, it typically needs as much storage as a pointer:
struct P
{
int* p;
};
struct R
{
int& r;
};
static_assert(sizeof(P) == sizeof(R), "sizeof(P) == sizeof(R)");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With