int main()
{
    int ival=12 , ival2 = 14;
    int &ref=ival;
    ref = ival2;
    cout<<ref;
    return 0;
}

That code shows an o/p as 14? How is it possible ? i thought references were not reassignable ?

1 more thing- do references occupy seperate spaces in memory like pointers ?


Consider this :

int ival;
int &ref = ival;
cout<<sizeof(ref);

In this case is the 'ref' replaced by ival ? or is it that ref occupies 4 bytes in the memory like ival ?


Thank ya

Recommended Answers

All 6 Replies

The output of the first piece of code is 14 because the value of variable ival (referred to by ref) is set to the value of ival2 (which is 14). A reference can only be set once, upon construction (line 4). Afterwards, any operation on the reference is exactly as if it was the variable it refers to. It is essentially like as if you just change the name of the variable. If you print out the value of ival too on line 6, you would also get 14.

>>1 more thing- do references occupy seperate spaces in memory like pointers ?
That depends on the case. In the example you posted, no, because the compiler is intelligent enough to know that ref is ival and there is no point in creating a "pointer" to ival (sort of encapsulated in the underlying implementation of the reference type). But under other circumstances (like getting a variable passed by reference to a function) it is more likely that the compiler will implement the reference with an underlying "pointer", but that depends how complex (inline or not, and the memory pattern on the stack) the situation is and how clever your compiler is.

For the second code snippet, I wasn't sure myself what a sizeof on a reference would output. So to feed my curiosity I tested this:

char ival = 12;
  char& ref = ival;
  std::cout << sizeof(int&) << " " << sizeof(ref) << " " << sizeof(ival) << std::endl;
  return 0;
//And the output is (32bit) : 4 1 1

So, you see, a reference is really treated as just a placeholder for the other variable, even in terms of the type (and its size).

You're not reassigning the reference, you're overwriting the value of ival, to which ref is a synonym:

#include <iostream>

int main()
{
    int ival = 12, ival2 = 14;
    int &ref = ival;

    ref = ival2;

    std::cout<< ref <<'\n'<< ival <<'\n';
}

>1 more thing- do references occupy seperate spaces in memory like pointers ?
In general, it's safe to assume that references do take up space under the hood, because otherwise how would the compiler maintain the relationship of reference->referent? Officially, it's unspecified by the language definition, which gives compilers leeway to optimize away references in certain cases. Your program, for example, could potentially be optimized as such:

#include <iostream>

int main()
{
    int ival = 12, ival2 = 14;
    //int &ref = ival;

    ival = ival2;

    std::cout<< ival <<'\n'<< ival <<'\n';
}

Aha thank you very much. The first question has been answered. I was rather confused.

However i am still not sure if a ref occupies space or not ? Is it really situation dependent and the compiler decides?

EDIT:

Ok for a basic assignment operation u need
- lvalue at left hand side
- rvalue at right hand side

int a =5;

So for

int ival, &ref=ival

i think ref may not be a separate memory location like a pointer since we are also using & (address of)operator ?

So as far as i know and with the little examples that i know references do not occupy memory .

Any1 can provide a code and proof that references occupy memory space?

It might be compiler dependent, I don't know, but in VC++ 2010 Express references do not occupy memory

void foo(int& ref)
{
    printf("foo: %p\n", &ref);
}

int main()
{
    int ival = 123;
    int& ref = ival;
    printf("%p %p\n", &ival, &ref);
    foo(ival);
}

And the output is

0014FAC8 0014FAC8
foo: 0014FAC8
Press any key to continue . . .

I tried that as well. The output addresses are all the same. Seems like reference is just a synonym.

>Is it really situation dependent and the compiler decides?
Yes.

>printf("%p %p\n", &ival, &ref);
This is a silly test to see if references occupy memory. Obviously due to the semantics of references, you're taking the address of the referent rather than the reference. I believe you'll find that Visual Studio uses a pointer internally if you include assembler output in your build.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.