CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.

Slides:



Advertisements
Similar presentations
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Advertisements

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.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
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.
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
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.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CS 1704 Introduction to Data Structures and Software Engineering.
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
Pointers *, &, array similarities, functions, sizeof.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Variables and memory addresses
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
C++ Functions A bit of review (things we’ve covered so far)
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Pointers What is the data type of pointer variables?
Motivation and Overview
Pointers.
Student Book An Introduction
Pointers and References
Andy Wang Object Oriented Programming in C++ COP 3330
Pointer Basics Psst… over there.
Built-In (a.k.a. Native) Types in C++
Overloading functions
Data Structures and Algorithms Introduction to Pointers
Pointer Basics Psst… over there.
Pointers and pointer applications
Introduction to Pointers
Presentation transcript:

CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object itself –Also known as “aliasing” Two ways to do this –Directly, via a reference Acts as an alias for the object User interacts with reference as if it were the object itself –Indirectly, via a pointer Gives the address (in memory) of the object Requires the user to do extra work: dereferencing

CSE 232: C++ pointers, arrays, and references Untangling Operator Syntax SymbolUsed in a declaration Used in a definition unary & (ampersand) reference int i = 3; int &r = i; address-of p = &i; unary * (star) pointer int * p; dereference (get what’s pointed to) *p = 7; -> (arrow) member access via pointer cp->add(3);. (dot) member access (same syntax for either reference or object) c.add(3); [] (square bracket)array dimensions int a[3]; array indexing cout << a[0] << endl;

CSE 232: C++ pointers, arrays, and references What’s a Reference in C++? A variable holding an address –Of what it “refers to” in memory But with a nicer interface –An alias to the object –Hides indirection from programmer Must be typed –Checked by compiler –Again can only refer to the type to which it can point int &r = i; // can only refer to int Must always refer to something –Must be initialized, cannot be changed –More restricted than Java references 0x7fffdad0 7 int i int &r

CSE 232: C++ pointers, arrays, and references Const References int main (int, char *[]) { const int i = 0; int j = 1; // non-const reference // r can’t refer to i int &r = j; // this is ok, though const int &s = i; const int &t = j; } Remember: references must refer to something –Can’t be 0 (or NULL) –Except through bad tricks like int *p = 0; int & r = *p; Also, once initialized, references cannot be changed –E.g., can’t redirect t to i –In Java, can re-assign references –In C++, you cannot Const reference –Promise not to change what’s aliased –E.g., can’t use t to change j Can’t have a non-const reference alias a const variable –Reverse is OK

CSE 232: C++ pointers, arrays, and references Exercise Write a simple main function –or just use the one provided in the array_exercise0.cc file Declare two integers –Give them different initial values –Make one of them const Declare four references –One const and one non-const reference initialized to alias each of the integers –What happens when you compile? –Comment out any reference declaration that’s illegal –What is printed when you insert print the names and values of the variables and the (remaining) references into cout? Use a non-const reference to change a value –What is printed when you for the same variables and references now? Do you understand why it prints what it prints each time?

CSE 232: C++ pointers, arrays, and references What’s a Pointer in C++? A variable holding an address –Of what it “points to” in memory Can be untyped void * v; // can point to any type However, usually they’re typed –Checked by compiler –Can only be assigned addresses of variables of the type to which it can point int * p; // can only point to int Addresses: garbage, something, nothing –When created: int * p = i; vs. int * q; q = 0; // now it points to nothing p = NULL; // not portable, use 0 instead 0x7fffdad0 7 int i int * p 3 const int j 7 int k

CSE 232: C++ pointers, arrays, and references Location and Value Comparisons Pointers may be compared for equality –Same as comparing addresses of what pointers point to (memory locations: l-values) Contents of what pointers point to may be compared, too (r-values) First implies second, not other way around p == q&*p == &*q *p == *q 0xefffdad0 5 int i int *p 5 int j 0xefffdbd0 int *q

CSE 232: C++ pointers, arrays, and references Const Pointers and Pointers to Const Types int main (int, char *[]) { const int i = 0; int j = 1; int k = 2; // pointer to int int * w = &j; // const pointer to int int * const x = &k; // pointer to const int const int * y = &i; // const pointer to const int const int * const z = &j; } Make promises via the const keyword in pointer declaration: –not to change pointer itself –not to change value it aliases –can also promise neither/both Read declarations right to left In this example, can change –w and what it points to –what x points to but not x –y but not what it points to –neither z nor what it points to A pointer to a non-const type cannot point to a const variable –w and x can’t point to i

CSE 232: C++ pointers, arrays, and references Exercise Write another simple main function Declare two integers –Give them different initial values –Make one of them const Declare four pointers, all initialized to 0 –Make one of them a pointer to integer –Make one of them a const pointer to integer –Make one of them a pointer to const integer –Make one of them a const pointer to const integer Try assigning addresses of integer variables –Which pointers cannot change at all? –Which pointer can point to either variable? –Which pointer can only point to one of the variables? As you change the pointers –Print out their names, addresses, and values of what they point to