Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a reference variable represented in memory?

int num = 0;
int *ptrNum = #
int &refNum = num;

enter image description here

Which one is the correct representation of reference variable in the memory-table 1 or table 2?
If table 2,then why a pointer is an object and a reference isn't?
If both representations are incorrect then please provide a correct representation and an explanation for why a reference isn't an object.

like image 748
Karan Joisher Avatar asked Oct 28 '25 14:10

Karan Joisher


1 Answers

Which one is the correct representation of reference variable in the memory-table 1 or table 2?

It could be either, or neither.

In this case, there is no need for storing the address of the object, so the table 1 would be enough. In another example, an address of the referenced variable could be needed. Such as when the reference is an argument of a non-inlined function.

please provide a correct representation

draft N4140 of the C++ standard [dcl.ref]/4:

It is unspecified whether or not a reference requires storage

How it could be stored is not specified at all.

an explanation for why a reference isn't an object.

The same document defines object as

[intro.object]/1

... An object is a region of storage ...

If a reference is not defined to have storage, then it's not defined to be an object.

like image 153
eerorika Avatar answered Oct 30 '25 05:10

eerorika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!