Reference Variables The symbol “&” has a few different purposes depending on where it occurs in code. When it appears in front of a variable name, it is.

Slides:



Advertisements
Similar presentations
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Advertisements

A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Run-Time Storage Organization
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 8 :: Subroutines and Control Abstraction
Lecture No.01 Data Structures Dr. Sohail Aslam
Runtime Environments Compiler Construction Chapter 7.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
C++ Pointers Review. Overview  What is a pointer  Why do I care?  What can be 'pointed to'?  Example.
Chapter 9 Classes: A Deeper Look, Part I Part II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Object Lifetime and Pointers
Lecture 7 Macro Review Stack Frames
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Run-Time Environments Chapter 7
Programming what is C++
User-Written Functions
Lecture No.09 Data Structures Dr. Sohail Aslam
Names and Attributes Names are a key programming language feature
Pointers.
COMPILERS Activation Records
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Programming Fundamentals Lecture #7 Functions
Pointers and Pointer-Based Strings
Pointers Revisited What is variable address, name, value?
Passing Arguments to a Function
Dynamic Memory Allocation
CSC 253 Lecture 8.
Chapter 9 :: Subroutines and Control Abstraction
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
User Defined Functions
7 Arrays.
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Pointers, Dynamic Data, and Reference Types
The const Keyword The answer is that, yes, we don’t want the function to change the parameter, but neither do we want to use up time and memory creating.
Memory Allocation CS 217.
Understanding Program Address Space
Pointers Call-by-Reference CSCI 230
Simulating Reference Parameters in C
UNIT V Run Time Environments.
7 Arrays.
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
Reference Variables One should be careful about transient objects that are stored by reference in data structures. Consider the following code that stores.
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
Lecture No.02 Data Structures Dr. Sohail Aslam
Pointers and dynamic objects
Pointers, Dynamic Data, and Reference Types
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Chapter 10 Def: The subprogram call and return operations of
Introduction to Pointers
Presentation transcript:

Reference Variables The symbol “&” has a few different purposes depending on where it occurs in code. 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; Start of lecture 17

Lecture No.17 Data Structure Dr. Sohail Aslam

Reference Variables The symbol “&’ can also appear after a type in a function signature: For example, insert and remove from the BinarySearchTree class. void insert( const EType& x ); void remove( const EType& x );

Reference Variables Or, in case we designed the BinarySearchTree class to hold integers only, i.e., no templates void insert( const int& x ); void remove( const int& x );

Reference Variables The “&” indicates a parameter that is a reference variable. Consider the following three different functions:

Reference Variables // example 1 int intMinus1( int oldVal) { oldVal = oldVal – 1; return oldVal; }

Reference Variables // example 2 int intMinus2( int* oldVal) { *oldVal = *oldVal – 2; return *oldVal; }

Reference Variables // example 3 int intMinus3( int& oldVal) { oldVal = oldVal – 3; return oldVal; }

Reference Variables The caller function: calling intMinus1 void caller() { int myInt = 31; int retVal; retVal = intMinus1( myInt ); cout << myInt << retVal; }

Memory Organization Process 1 (browser) Code Process 3 (word) Static data Process 4 (ourtest.exe) Stack Process 2 (dev-c++) Windows OS Heap

Reference Variables Call stack layout Parameters(caller) Local variables(caller) Return address(caller) Parameters(intMinus1) Local variables(intMinus1) Return address(intMinus1) sp Stack grows downwards

Reference Variables Call stack layout when intMinus1 is called: 1072 31 myInt ? retVal 1068 calling function “caller” caller’s other stuff The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 31 oldVal 1060 1056 called function “intMinus1” 1052 sp stack grows downwards

Reference Variables How are myInt and oldVal related? Passing myInt to the function intMinus1 results in a copy of myInt to be placed in parameter oldVal in the call stack. Alterations are done to the copy of “31” (stored in oldVal) and not the original myInt.

Reference Variables The original myInt remains unchanged. For this reason, this technique of passing parameters is called pass by value. Alterations are done to the copy of “31” (stored in oldVal) and not the original myInt.

Reference Variables Call stack layout after subtraction in intMinus1: 1072 31 myInt ? retVal 1068 calling function “caller” caller’s other stuff The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 31 30 oldVal 1060 1056 called function “intMinus1” 1052 sp stack grows downwards

calling function “caller” Reference Variables Call stack layout after return from intMinus1: myInt 1072 31 30 retVal 1068 calling function “caller” caller’s other stuff The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. sp stack grows downwards

Reference Variables We could have called intMinus1 as void caller() { int retVal; retVal = intMinus1( 31 );// literal cout << myInt << retVal; } Because it is the value that is passed. We can always pass a literal or even an expression in call-by-value.

Reference Variables If the programmer wanted to actually change a variable’s value from within a function, one way would be to send a pointer: void caller() { int retVal; int myInt = 31; retVal = intMinus2( &myInt ); cout << myInt << retVal; } Call this call-by-pointer.

Reference Variables Call stack layout when intMinus2 is called: 1072 31 myInt ? retVal 1068 calling function “caller” caller’s other stuff The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 1072 oldVal 1060 1056 called function “intMinus2” 1052 sp stack grows downwards

Reference Variables Call stack layout after *oldVal = *oldVal – 2; 31 29 myInt 1072 ? retVal 1068 calling function “caller” caller’s other stuff The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 1072 oldVal 1060 1056 called function “intMinus2” 1052 sp stack grows downwards

calling function “caller” Reference Variables Call stack layout after return from intMinus2. 29 myInt 1072 31 29 retVal 1068 calling function “caller” caller’s other stuff End of lecture 2 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. sp stack grows downwards

Reference Variables Suppose we want a function to change an object. But we don’t want to send the function a copy. The object could be large and copying it costs time. We don’t want to use pointers because of the messy syntax.

Reference Variables The answer: call-by-reference (or pass-by-reference): void caller() { int retVal; int myInt = 31; retVal = intMinus3( myInt ); cout << myInt << retVal; }

Reference Variables The & after int means that oldVal is an integer reference variable. // example 3 int intMinus3( int& oldVal) { oldVal = oldVal – 3; return oldVal; }

Reference Variables So what is a reference variable? 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. The function intMinus3 cannot use the name myInt because it is in the caller’s scope. But both variable names refer to the same object (same memory cell).

Reference Variables Call stack layout when intMinus3 is called: 31 myInt 1072 ? retVal 1068 calling function “caller” caller’s other stuff oldVal 1060 1056 called function “intMinus3” 1052 sp stack grows downwards

Reference Variables Call stack layout when intMinus3 is called: oldVal 1072 31 myInt ? retVal 1068 calling function “caller” caller’s other stuff The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 1060 1056 called function “intMinus3” 1052 sp stack grows downwards

Reference Variables Call stack layout after oldVal = oldVal - 3: 31 28 myInt 1072 ? retVal 1068 calling function “caller” caller’s other stuff The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 1060 1056 called function “intMinus3” 1052 sp stack grows downwards

calling function “caller” Reference Variables Call stack layout after return from intMinus3: 31 28 myInt 1072 28 retVal 1068 calling function “caller” caller’s other stuff The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. sp stack grows downwards

Reference Variables The compiler may actually implement call-by-reference using pointers as we did in example 2. The obtaining of address and de-referencing would be done behind the scene. We should think in terms of the “renaming” abstraction. End of Lecture 17