Reference Variables One should be careful about transient objects that are stored by reference in data structures. Consider the following code that stores.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
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.
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Pointers “Absolute C++” Section 10.1
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Run-Time Storage Organization
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
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.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
February 11, 2005 More Pointers Dynamic Memory Allocation.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Variables and memory addresses
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Memory Management.
Popping Items Off a Stack Using a Function Lesson xx
Lecture No.09 Data Structures Dr. Sohail Aslam
Run-time organization
Pointers and Pointer-Based Strings
Student Book An Introduction
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Dynamic Memory Allocation
CSC 253 Lecture 8.
Chapter 9 :: Subroutines and Control Abstraction
Lecture 21 Stacks and Queues Richard Gesick.
CSC 253 Lecture 8.
User Defined Functions
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Memory Organization Process 1 (browser) Code Process 3 (word)
System Structure and Process Model
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
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.
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.
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Dynamic Memory A whole heap of fun….
Pointers Pointers point to memory locations
Pointers and Pointer-Based Strings
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Data Structures and Algorithms Introduction to Pointers
Dynamic Memory And Objects
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers and dynamic objects
Object-Oriented Programming (Part 2)
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Reference Variables One should be careful about transient objects that are stored by reference in data structures. Consider the following code that stores and retrieves objects in a queue.

Reference Variables void loadCustomer( Queue& q) { Customer c1(“irfan”); Customer c2(“sohail”; q.enqueue( c1 ); q.enqueue( c2 ); }

Lecture No.18 Data Structure Dr. Sohail Aslam

Reference Variables We got the reference but the object is gone! void serviceCustomer( Queue& q) { Customer c = q.dequeue(); cout << c.getName() << endl; } We got the reference but the object is gone! The objects were created on the call stack. They disappeared when the loadCustomer function returned.

Reference Variables void loadCustomer( Queue& q) { Customer* c1 = new Customer(“irfan”); Customer* c2 = new Customer(“sohail”; q.enqueue( c1 ); // enqueue takes pointers q.enqueue( c2 ); } The pointer variables c1 and c2 are on the call stack. They will go but their contents (addresses) are queued. The Customer objects are created in the heap. They will live until explicitly deleted.

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 when q.enqueue(c2) called in loadCustomer. elt 600 c1 1072 624 c2 1068 loadCustomer . End of lecture 2 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. (624) (elt) 1060 1056 enqueue 1052 sp stack grows downwards

Reference Variables Heap layout during call to loadCustomer. sohail heap grows upwards c1 648 c2 sohail Customer(“sohail”) -> c2 End of lecture 2 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 624 irfan Customer(“irfan”) -> c1 600

Reference Variables void serviceCustomer( Queue& q) { Customer* c = q.dequeue(); cout << c->getName() << endl; delete c; // the object in heap dies } Must use the c-> syntax because we get a pointer from the queue. The object is still alive because it was created in the heap.

The const Keyword The const keyword is often used in function signatures. The actual meaning depends on where it occurs but it generally means something is to held constant. Here are some common uses.

The const Keyword Use 1: The const keyword appears before a function parameter. E.g., in a chess program: int movePiece(const Piece& currentPiece) The parameter must remain constant for the life of the function. If you try to change the value, e.g., parameter appears on the left hand side of an assignment, the compiler will generate and error.

The const Keyword This also means that if the parameter is passed to another function, that function must not change it either. Use of const with reference parameters is very common. This is puzzling; why are we passing something by reference and then make it constant, i.e., don’t change it? Doesn’t passing by reference mean we want to change it? End of lecture 18