Professional Documents
Culture Documents
Lecture No.
17
___________________________________________________________________
Data Structures
Lecture No. 17
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 4
Summary
Reference Variables
Sample Program
After demonstrating the use of const and reference variables in the interface class for a
binary search tree in the previous lectures, we will now see what these reference
variables are and how does internal process go on during the use of reference
variables.
Reference Variables
Before proceeding ahead, there is need to know why the reference variables are used;
and what benefits a programmer can get while employing them. We will discuss these
all things in detail with the help of examples.
The symbol &, used for reference variable has a few different purposes with respect to
its occurrence in the code. In C++ programming, we have seen that when the
ampersand sign i.e. & appears in front of a variable name, it is the address operator. It
returns the address of the variable in front of which it is written. Thus for example, if x
is a variable name, then &x ; will return the address of the variable x. In general we can
say that
&variablename ;
will return the address of the variable. We know that an address can be stored in a
pointer. To further understand this concept, lets suppose that there are following lines
in our code.
int x ;
int* ptr = &x;
The first line declares a variable x of type int while the second one declares a pointer to
int and assigns it the address of x. This address of variable x is returned by the & sign
written in front of the variable x. Thus a pointer variable is initialized by assigning it the
address of a variable. This address of the variable is gotten by using the & sign with the
variable name.
The other place where & sign can appear is the signature of the function where it
appears after the type of the parameter. Consider the insert and remove methods from
BinarySearchTree class that were declared as the following.
Page 1 of 14
Process 1
(browser)
Code
Process 3
(Word)
Static data
Stack
Process 4
(ourtest.exe)
Process 2
(Dev-C++)
Windows Os
Heap
sp
Parameters (intMinus1)
Local variables(intMinus1)
Return address
(intMinus1)
In the stack layout figure, there are the entries of the caller in the upper portion. This is
due to the fact that the caller function itself was called (executed) by some one else.
The lower portion of the stack describes the stack status when the function intMinus1
is called. The stack contains parameters, local variables and return address of the
function intMinus1. There is a stack pointer sp that points to the top of the stack (the
figure shows the stack downward; it should not be confused with the top). As the
caller has called intMinus1, this function is on the top of the stack.
The following figure shows the contents of the stack when the function intMinus1 is
called.
1072
1068
31
?
myInt
retVal
callers other
stuff
1060
31
oldVal
1056
1052
sp
stack grows downward
Page 5 of 14
31
?
1068
myInt
retVal
callers other
stuff
1060
31
30
oldVal
1056
1052
sp
stack grows downward
31
30
myInt
retVal
callers other
stuff
sp
Page 6 of 14
1072
1068
31
?
myInt
retVal
callers other
stuff
1060
oldVal
1072
1056
1052
sp
stack grows downward
31 29
?
myInt
retVal
callers other
stuff
1060
1072
oldVal
1056
1052
sp
stack grows downward
Page 8 of 14
1072
1068
31
29
29
myInt
retVal
callers other
stuff
sp
Page 9 of 14
31
?
myInt
retVal
callers other
stuff
oldVal
1060
1056
1052
sp
stack grows downward
Page 10 of 14
The caller function part of the stack contains myInt, retVal and other stuff of the caller
function. In the stack part intMinus3, the name oldVal shows that it has nothing in this
portion but it is the other name of the memory location 1072, named as myInt in the
caller function. The dotted arrow shows this.
In the following figure 17.10, we show the oldVal along with the myInt at same
memory location. Here we want to show that the oldVal of called function and myInt
of caller function are the two names of the same memory location.
OldVal
31
?
1072
1068
myInt
retVal
callers other
stuff
1060
1056
1052
sp
stack grows downward
1072
1068
31 28
?
myInt
retVal
callers other
stuff
1060
1056
1052
sp
stack grows downward
Page 11 of 14
Fig 17.11: call stack layout after oldVal = oldVal - 3
;
Now when the function intMinus3 returns, the returned value (that is 28) is written in
retVal and the stuff (activation record) of intMinus3 is removed. The stack layout
becomes as shown in the figure below.
1072
31
1068
28
28
myInt
retVal
callers other
stuff
sp
Sample Program
Following is the program, which demonstrate the above three function calls that we
discussed in the previous example. We define the three functions intMinus1, intMinus2
and intMinus3. These functions accept the argument as value, pointer and reference
variable respectively. The endl puts a new line and is used in the program for the
output clarity. Here is the code of the program followed by the output of the program.
/*This program demonstrate tha how the value in a caller function is effected when it is
passed to a function by using call by value, by using pointers and by using call by
reference methods.
*/
#include <iostream.h>
//Function 1, call by value
int intMinus1( int oldVal)
{
oldVal = oldVal 1;
return oldVal;
Page 12 of 14
Page 13 of 14
Page 14 of 14