LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-04-2004, 02:47 AM   #1
atul_mehrotra
LQ Newbie
 
Registered: Sep 2004
Posts: 28

Rep: Reputation: 15
reference Variable in C++


Hii guys,
i had just shifted frm C to C++..I wanna ask abt reference variables...
I wanna know abt how the compiler manages Reference variable..
Look when v talk abt "pass by Value " a new copy of the variable is created on stack.
And when v talk abt "Pass by Address" the Address of the variable is passed to new function on stack.
i wanna ask what happens in stack when v say abt "Pass by Reference " in C++
thanx...
 
Old 10-04-2004, 03:12 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
The actual implementation may differ between compilers, but I believe that it's common to pass the address of the variable, as a reference is basically a pointer that has been implicitly dereferenced.
 
Old 10-04-2004, 03:18 AM   #3
atul_mehrotra
LQ Newbie
 
Registered: Sep 2004
Posts: 28

Original Poster
Rep: Reputation: 15
this means u wanna say that for "refernce variables" also the address of the variable is passed on the stack........then y v use REference variables if the same sort of job can b done with pointers....
 
Old 10-04-2004, 03:48 AM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
I assume you mean “why would you use references instead of pointers”?

Reference variables cannot point to NULL, unlike pointers. This eliminates the erroneous case where you pass a NULL value to a function (method) that expects a valid pointer. Also, it helps with typesafing; it's easy to end up with typeless void * pointers, but you can't have a void reference; if you try and cast a reference to another type the compiler will trap the error (unless you an operator is defined to tell it how to do that).

Everything you can do with references can be done with pointers.
 
Old 10-04-2004, 05:00 AM   #5
atul_mehrotra
LQ Newbie
 
Registered: Sep 2004
Posts: 28

Original Poster
Rep: Reputation: 15
But buddy i just wanna know how the C++ compiler actually handles these reference variables....
U can take example of GCC or TURBOC anything...but i wanna know abt the actual handling of reference variables...

Last edited by atul_mehrotra; 10-04-2004 at 05:04 AM.
 
Old 10-04-2004, 05:35 AM   #6
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
What do you mean handles? A reference variable is an alias to another variable a. What happens to the alias is reflected in a and vice versa. Under the hood, many compilers implement this using pointers. If you want to know exactly how you can always check GCC's source code, but I doubt you'd understand much.
 
Old 10-06-2004, 10:49 AM   #7
silvercl
LQ Newbie
 
Registered: Oct 2004
Location: pune, india
Distribution: rh9
Posts: 1

Rep: Reputation: 0
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.

Last edited by silvercl; 10-25-2004 at 06:32 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 02:18 PM
calling a variable within a variable sdandeker Programming 9 04-28-2004 03:55 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration