Hi guys,
{PTR => pointer}
I found out lots of related replies to this Q here, related to PTRs
And its true, that the reference variables are accessed/derefernced as ptrs.
But, there are some differences and why so ?, let me explain...
Following are some excerpts of the dissassembled code of the 'xyz.cc' file thru "g++ -S"
Code:
----CODE---with CASES-----
int i;
int foo(char & ch_ref){
1: int & i_ref = i;
// movl $i, -4(%ebp) <<<<===== here we have addr of "i"
// from the global space,
// so, its "some constant
// (literal) value" being moved
-------
2: int * i_ptr = & i_ref;
// movl -4(%ebp), %eax <<<=== here value holded by "i_ref"
// is being moved
//
// movl %eax, -8(%ebp)
-------
3: putchar(ch_ref);
// movl 8(%ebp), %eax
// movsbl (%eax),%eax <<<=== quite clear abt the dereferencing(PTR)
// pushl %eax
// call putchar
-------
4: return 0;
} /* end of foo*/
----------------------CODE----done
case-1: --> shows how the compile time addresses are being moved/sent/used
case-2: --> shows how the referring to a reference with "&" {addr of} is implemented internally
case-3: --> finally, shows the dereferencing of aliases/ref variables
So, from above its quite clear that the compiler dereferences the reference variables.
Now, an "alias" is like the "EQU" assembler directive. {Now, what is this EQU ? It's a mnemonic for EQUAL}
so when I have declared a address for "int i;" // addr_of_i = 0x80001234
and I say "int & i_ref = i" implies // EQU i_ref i ; /* alias "i" as "i_ref" */
So, if you have addr of a variable at compile time (like those of globals or statics), you can easily alias variables.
But, now tell me how would you, for those of which you don't know or you access them thru the stack_mechanism like ebp+2 or ebp-4 ... ?
So, you need the pointers there. Here comes the entry for pointers, and this is why (i guess) the compilers finally handle the reference variables as the pointers internally.
So finally, compiler internally plays with the "address of variable" when we use reference variables :(
See this is bcoz, when you pass on a variable as an argument then, tell me how could you alias it, since its address is not present before hand.
And the variable on stack are almost like ptrs, aren't they ? So, its the same.
Yeah finally, programmer is playing with the aliases since he doesn't need/care for the transparence.
Now, the IMPORTANT thing WHY REFERENCERS even though we have PTRs ?
1) ONCE you initialise the reference variable, and you try to refer its address will give
its value holded (in case of stack variables {CASE-2 see above code}) or
numeric/literal/constant value(in case of compile time addresses available for global
space variables {CASE-1 see above code}). So, you CANNOT change them or their
value(although you can change the referred variables value, etc), now don;t ask me
how, you shd be smart to do that ;)
2) using a ref var is more easy than derefferencing a ptr FOR a programmer :)
So, I guess you all are satisfied.
Do post for any correction of the above info :)
-SilverZ.