Where is the reference variable stored

I know that this link does not require any memory, it will point to the same memory cell to which it refers. eg,

int i=10; int &r = a; 

suppose i points to memory location 1000, so in this case r also points to memory location 1000. But in C ++, when we declare a variable, its memory will be stored in memory in a specific place. In this case, r points to some place, but it should be stored somewhere in memory, since the internal representation is for reference use only. thanks in advance.

+6
source share
4 answers

This remains unspecified and not without reason. The real answer: it depends on the link. It can be represented as a regular pointer, or it can be absent altogether.

If you have a local link function with automatic storage duration, for example r :

 void foo() { int x[4] = {0, 1, 2, 3}; int &r = x[1]; // more code } 

then he probably won’t take a seat at all. The compiler will simply consider all uses of r as an alias for x[1] and access this int directly. Please note that such alias-style links can also occur as a result of creating a function.

On the other hand, if the link is β€œpermanent” or visible to other translation units (such as a data item or global variable), it should take up some space and be stored somewhere. In this case, it will most likely be represented as a pointer, and its code will be compiled to dereference that pointer.

Other options are theoretically possible (for example, a lookup table), but I do not think that they are used by the real-world compiler.
+6
source

I know this link does not require any memory

Not really. Whether the link is a repository is not specified. This may or may not be the case. In this particular example, it does not need storage, so it does not use a typical implementation.

it will point to the same memory location that it refers to

It sounds like a tautology or just a misunderstanding, depending on what you mean by a "dot". A link refers to an object or is tied to an object. You can think of it as an alias of a variable name. The variable name also does not use memory.

In this case, r points to some place, but it should be stored somewhere in memory

It does not need to be stored in memory. Consider the following code:

 int i=10; int &r = a; int j = r * 3; 

The compiler can interpret r * 3 as i * 3 , as if you wrote it first. The location of the mentioned object is known at compile time, so there is no need to store the address in memory, which is the runtime.

But in other situations, storage may be required. For example: Consider a reference argument to a non-built-in function that has an external connection. The specified object cannot be known when compiling the function, so some information must be passed in memory at run time.

as an internal representation for reference only use const pointer

It is not right. An internal representation may use a pointer, or it may use something else, or it may not need anything.

So, to answer briefly

Where is the reference variable stored

Not specified. Nowhere, not anywhere.

+1
source

What the standard says:

It is not indicated whether reference (3.7) is required for storage.

(C ++ 11, [dcl.ref] ΒΆ4)

This means that the compiler can choose for each case, regardless of whether any storage is required.

Now let people say what they want, but links come down to syntactic sugar for pointers (even at the compiler level in all major C ++ compilers, the concept of β€œlink” disappears almost immediately after the interface); thus, in the general case, they may need their space in memory, just like a pointer. However, in cases such as yours (local links), the compiler should look at them and optimize them as necessary.

Please note that this is not an exception to the links - the compiler is able to perform the same optimization even with pointers (as soon as your code goes into SSA, there is nothing special even if the links cannot be reinstalled).

It:

 int glob; void direct() { glob = 16; } void through_reference() { int &a = glob; a = 16; } void through_pointer() { int *a = &glob; *a = 16; } 

always boils down to the same code on any compiler I tried on gcc.godbolt.org - an example :

 direct(): mov DWORD PTR glob[rip], 16 ret through_reference(): mov DWORD PTR glob[rip], 16 ret through_pointer(): mov DWORD PTR glob[rip], 16 ret glob: .zero 4 

On the other hand, the situation becomes a little more slippery when it comes to structures; here, the compiler is allowed to kill links away from the actual location of the structure (if it is able to restore what they actually point to), while for pointers the situation can be a little more complicated (their elite will violate the classes of the standard layout material).

In practice, I have never seen such optimization implemented in any real compiler. Take gcc or MSVC or Clang or something else, and you will always see that the size of the structure will be equal even in the most trivial cases .

0
source

as an internal representation for reference only use const pointer

Where did you hear that? It is not true.

The standard does not indicate how links are executed.

The following is too simplistic

Most often, the compiler has an internal symbol table that stores all the information needed for the variables.

Take a simple case:

 int a; a = 100; 

then the compiler may have something like this (simplicity allows you to make address a fixed known address)

 | identifier | type | address | |------------|------|----------| | a | int | 0xFF00A4 | 

Then it can convert the C ++ code to something like this:

 mov 0xFF00A4, 100 

Add the link to the mix:

 int a; a = 100; int& ra = 300; 

Character table containing the compiler:

 | identifier | type | address | |------------|------|----------| | a | int | 0xFF00A4 | | ra | int& | 0xFF00A4 | 

Or:

 | identifier | type | address | alias | |------------|------|----------|-------| | a | int | 0xFF00A4 | - | | ra | int& | - | a | 

And therefore, it can generate code as follows:

 mov 0xFF00A4, 100 mov 0xFF00A4, 300 

When you have a reference argument in a function, then the pointer is passed inside.

0
source

All Articles