views:

591

answers:

6

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
+2  A: 

References don't actually exist physically until they need to have a physical manifestation (i.e., as a member of an aggregate).

Having an array of references is illegal probably due to the above. But nothing prevents you from creating an array of structs/classes that have reference members.

I'm sure someone will point out the standard clause that mentions all this.

MSN
+13  A: 

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?

AraK
This kind of dodges the question of why `j` doesn't take up any stack space. If it was *just* "the address of i" it would take up `sizeof(int*)` bytes.
jalf
It doesn't take up any stack because the compiler *knows* the address of i. It doesn't need to store it.
Peter Ruderman
You can think of a reference variable as being a synonym for another variable. It doesn't require more storage because it isn't a real "thing", just a new name for an existing thing.A reference argument, on the other hand, is essentially a pointer value and requires the memory of a pointer.
Darryl
point is it is not simply "the address of i". It is *another name* for i. In some cases, this "other name" has to be implemented as a pointer, by storing the address of i, which takes up a few bytes, but that's an implementation detail, not part of the concept of a reference.
jalf
+5  A: 

You can't define an array of references because there is no syntax to initialize them. C++ does not allow uninitialized references. As for your first question, the compiler is under no obligation to allocate space for unnecessary variables. There is no way to have j point to another variable, so it's effectively just an alias for i in the function's scope, and that's how the compiler treats it.

Peter Ruderman
+6  A: 

In practice, a reference is equivalent to a pointer, except that the extra constraints on how references are allowed to be used can allow a compiler to "optimize it away" in more cases (depending on how smart the compiler is, its optimization settings, etc etc of course).

Alex Martelli
+9  A: 

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.

jalf
+1. In ISO C++, "reference is not an object". As such, it needs not have any memory representation. It's just an alias.
Pavel Minaev
+3  A: 

Something that is only mentioned in passing elsewhere - how to get the compiler to devote some storage space to a reference:

class HasRef
{
    int &r;

public:
    HasRef(int &n)
        : r(n) { }
};

This denies the compiler the opportunity to simply treat it as a compile-time alias (an alternative name for the same storage).

Daniel Earwicker