Upcoming SlideShare
×

# Computer notes - Reference Variables

343 views

Published on

The “&” indicates a parameter that is a reference variable. Because it is the value that is passed. We can always pass a literal or even an expression in call-by-value

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
343
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
24
0
Likes
0
Embeds 0
No embeds

No notes for slide
• Start of lecture 17
• The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
• The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
• The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
• The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
• The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
• End of lecture 2 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
• The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
• The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
• The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
• End of Lecture 17
• ### Computer notes - Reference Variables

1. 1. Class No.17 Data Structures http://ecomputernotes.com
2. 2. Reference Variables <ul><li>The symbol “&” has a few different purposes depending on where it occurs in code. </li></ul><ul><li>When it appears in front of a variable name, it is the address operator, i.e., it returns the address of the variable in memory. int x; int* ptr = &x; </li></ul>http://ecomputernotes.com
3. 3. Reference Variables <ul><li>The symbol “&’ can also appear after a type in a function signature: </li></ul><ul><li>For example, insert and remove from the BinarySearchTree class. void insert( const EType& x ); </li></ul><ul><li>void remove( const EType& x ); </li></ul>http://ecomputernotes.com
4. 4. Reference Variables <ul><li>Or, in case we designed the BinarySearchTree class to hold integers only, i.e., no templates void insert( const int& x ); </li></ul><ul><li>void remove( const int& x ); </li></ul>http://ecomputernotes.com
5. 5. Reference Variables <ul><li>The “&” indicates a parameter that is a reference variable . </li></ul><ul><li>Consider the following three different functions: </li></ul>http://ecomputernotes.com
6. 6. Reference Variables <ul><li>// example 1 </li></ul><ul><li>int intMinus1( int oldVal) </li></ul><ul><li>{ </li></ul><ul><li>oldVal = oldVal – 1; </li></ul><ul><li>return oldVal; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
7. 7. Reference Variables <ul><li>// example 2 </li></ul><ul><li>int intMinus2( int* oldVal) </li></ul><ul><li>{ </li></ul><ul><li>*oldVal = *oldVal – 2; </li></ul><ul><li>return *oldVal; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
8. 8. Reference Variables <ul><li>// example 3 </li></ul><ul><li>int intMinus3( int& oldVal) </li></ul><ul><li>{ </li></ul><ul><li>oldVal = oldVal – 3; </li></ul><ul><li>return oldVal; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
9. 9. Reference Variables <ul><li>The caller function: calling intMinus1 </li></ul><ul><li>void caller() </li></ul><ul><li>{ </li></ul><ul><li>int myInt = 31; </li></ul><ul><li>int retVal; </li></ul><ul><li>retVal = intMinus1( myInt ); </li></ul><ul><li>cout << myInt << retVal; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
10. 10. Memory Organization Code Static data Stack Heap Process 1 (browser) Process 3 (word) Process 4 (ourtest.exe) Windows OS Process 2 (dev-c++) http://ecomputernotes.com
11. 11. Reference Variables <ul><li>Call stack layout </li></ul>Parameters(caller) Local variables(caller) Return address(caller) Parameters(intMinus1) Local variables(intMinus1) Return address(intMinus1) Stack grows downwards http://ecomputernotes.com sp
12. 12. Reference Variables <ul><li>Call stack layout when intMinus1 is called: </li></ul>1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 31 calling function “caller” called function “intMinus1” stack grows downwards sp
13. 13. Reference Variables <ul><li>How are myInt and oldVal related? </li></ul><ul><li>Passing myInt to the function intMinus1 results in a copy of myInt to be placed in parameter oldVal in the call stack. </li></ul><ul><li>Alterations are done to the copy of “31” (stored in oldVal ) and not the original myInt . </li></ul>http://ecomputernotes.com
14. 14. Reference Variables <ul><li>The original myInt remains unchanged . </li></ul><ul><li>For this reason, this technique of passing parameters is called pass by value . </li></ul><ul><li>Alterations are done to the copy of “31” (stored in oldVal ) and not the original myInt . </li></ul>http://ecomputernotes.com
15. 15. Reference Variables <ul><li>Call stack layout after subtraction in intMinus1 : </li></ul>1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 31 calling function “caller” called function “intMinus1” stack grows downwards sp 30
16. 16. Reference Variables <ul><li>Call stack layout after return from intMinus1 : </li></ul>1068 31 1072 30 caller’s other stuff myInt retVal calling function “caller” stack grows downwards sp
17. 17. Reference Variables <ul><li>We could have called intMinus1 as </li></ul><ul><ul><li>void caller() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>int retVal; </li></ul></ul><ul><ul><li>retVal = intMinus1( 31 );// literal </li></ul></ul><ul><ul><li>cout << myInt << retVal; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Because it is the value that is passed. We can always pass a literal or even an expression in call-by-value. </li></ul>http://ecomputernotes.com
18. 18. Reference Variables <ul><li>If the programmer wanted to actually change a variable’s value from within a function, one way would be to send a pointer: </li></ul><ul><ul><li>void caller() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>int retVal; </li></ul></ul><ul><ul><li>int myInt = 31; </li></ul></ul><ul><ul><li>retVal = intMinus2( &myInt ); </li></ul></ul><ul><ul><li>cout << myInt << retVal; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Call this call-by-pointer. </li></ul>http://ecomputernotes.com
19. 19. Reference Variables <ul><li>Call stack layout when intMinus2 is called: </li></ul>1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 1072 calling function “caller” called function “intMinus2” stack grows downwards sp
20. 20. Reference Variables <ul><li>Call stack layout after *oldVal = *oldVal – 2; </li></ul>1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal 1072 calling function “caller” called function “intMinus2” stack grows downwards sp 29
21. 21. Reference Variables <ul><li>Call stack layout after return from intMinus2. </li></ul>1068 31 1072 29 caller’s other stuff myInt retVal calling function “caller” stack grows downwards sp 29
22. 22. Reference Variables <ul><li>Suppose we want a function to change an object. </li></ul><ul><li>But we don’t want to send the function a copy. The object could be large and copying it costs time. </li></ul><ul><li>We don’t want to use pointers because of the messy syntax. </li></ul>http://ecomputernotes.com
23. 23. Reference Variables <ul><li>The answer: call-by-reference (or pass-by-reference): </li></ul><ul><ul><ul><li>void caller() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>int retVal; </li></ul></ul></ul><ul><ul><ul><li>int myInt = 31; </li></ul></ul></ul><ul><ul><ul><li>retVal = intMinus3( myInt ); </li></ul></ul></ul><ul><ul><ul><li>cout << myInt << retVal; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>http://ecomputernotes.com
24. 24. Reference Variables <ul><li>The & after int means that oldVal is an integer reference variable. </li></ul><ul><ul><li>// example 3 </li></ul></ul><ul><ul><li>int intMinus3( int& oldVal) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>oldVal = oldVal – 3; </li></ul></ul><ul><ul><li>return oldVal; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com
25. 25. Reference Variables <ul><li>So what is a reference variable? </li></ul><ul><li>The idea is: the integer object myInt is used exactly as it exists in the caller. The function simply reaches it through a different name , oldVal . </li></ul><ul><li>The function intMinus3 cannot use the name myInt because it is in the caller’s scope. </li></ul><ul><li>But both variable names refer to the same object (same memory cell). </li></ul>http://ecomputernotes.com
26. 26. Reference Variables <ul><li>Call stack layout when intMinus3 is called: </li></ul>1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards sp
27. 27. Reference Variables <ul><li>Call stack layout when intMinus3 is called: </li></ul>1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards sp
28. 28. Reference Variables <ul><li>Call stack layout after oldVal = oldVal - 3 : </li></ul>1052 1060 1068 1056 31 1072 ? caller’s other stuff myInt retVal oldVal calling function “caller” called function “intMinus3” stack grows downwards sp 28
29. 29. Reference Variables <ul><li>Call stack layout after return from intMinus3 : </li></ul>1068 31 1072 28 caller’s other stuff myInt retVal calling function “caller” stack grows downwards sp 28
30. 30. Reference Variables <ul><li>The compiler may actually implement call-by-reference using pointers as we did in example 2. </li></ul><ul><li>The obtaining of address and de-referencing would be done behind the scene. </li></ul><ul><li>We should think in terms of the “renaming” abstraction. </li></ul>http://ecomputernotes.com