Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a C++ reference look, memory-wise?

Given:

int i = 42; int j = 43; int k = 44; 

By looking at the variables addresses we know that each one takes up 4 bytes (on most platforms).

However, considering:

int i = 42; int& j = i; int k = 44; 

We will see that variable i indeed takes 4 bytes, but j takes none and k takes again 4 bytes on the stack.

What is happening here? It looks like j is simply non-existent in runtime. And what about a reference I receive as a function argument? That must take some space on the stack...

And while we're at it - why can't I define an array or references?

int&[] arr = new int&[SIZE]; // compiler error! array of references is illegal 
like image 552
Yuval Adam Avatar asked Jul 24 '09 20:07

Yuval Adam


People also ask

Are references stored in memory?

A reference refers to the object or is bound to the object. You can consider it an alias of the variable name. The variable name doesn't use any memory either. It doesn't need to be stored in memory.

How much memory is a reference?

Assuming 64-bit, every reference is 8 bytes (a pointer).

Is a reference a memory address?

In computing, a memory address is a reference to a specific memory location used at various levels by software and hardware. Memory addresses are fixed-length sequences of digits conventionally displayed and manipulated as unsigned integers.

Does reference Take memory in C++?

Yes. Inside it is the same as a pointer. It will take 4 bytes on 32 bit platforms or 8 bytes on 64 bit platforms. Normally a local reference variable will be in a register, though it can be pushed to the program stack (RAM) if register demand requires it.


2 Answers

everywhere the reference j is encountered, it is replaced with the address of i. So basically the reference content address is resolved at compile time, and there is not need to dereference it like a pointer at run time.

Just to clarify what I mean by the address of i :

void function(int& x) {     x = 10; }  int main() {     int i = 5;     int& j = i;      function(j); } 

In the above code, j should not take space on the main stack, but the reference x of function will take a place on its stack. That means when calling function with j as an argument, the address of i that will be pushed on the stack of function. The compiler can and should not reserve space on the main stack for j.

For the array part the standards say ::

C++ Standard 8.3.2/4:

There shall be no references to references, no arrays of references, and no pointers to references.

Why arrays of references are illegal?

like image 107
Khaled Alshaya Avatar answered Nov 20 '22 03:11

Khaled Alshaya


How does a C++ reference look, memory-wise?

It doesn't. The C++ standard only says how it should behave, not how it should be implemented.

In the general case, compilers usually implement references as pointers. But they generally have more information about what a reference may point to, and use that for optimization.

Remember that the only requirement for a reference is that it behaves as an alias for the referenced object. So if the compiler encounters this code:

int i = 42; int& j = i; int k = 44; 

what it sees is not "create a pointer to the variable i" (although that is how the compiler may choose to implement it in some cases), but rather "make a note in the symbol table that j is now an alias for i."

The compiler doesn't have to create a new variable for j, it simply has to remember that whenever j is referenced from now on, it should really swap it out and use i instead.

As for creating an array of references, you can't do it because it'd be useless and meaningless.

When you create an array, all elements are default-constructed. What does it mean to default-construct a reference? What does it point to? The entire point in references is that they re initialized to reference another object, after which they can not be reseated.

So if it could be done, you would end up with an array of references to nothing. And you'd be unable to change them to reference something because they'd been initialized already.

like image 43
jalf Avatar answered Nov 20 '22 02:11

jalf