Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Similar documents
G52CPP C++ Programming Lecture 13

C++ (Non for C Programmer) (BT307) 40 Hours

CSE 307: Principles of Programming Languages

a data type is Types

Memory and C++ Pointers

18. Dynamic Data Structures I. Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

The paramaterless ctor (aka default ctor)

Introduction to C++ with content from

Lesson 13 - Vectors Dynamic Data Storage

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

COMP 2355 Introduction to Systems Programming

EECS 211 Lab 6. Classes and Abstractation Winter Getting the code. Encapsulation and Abstraction. Classes

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Question: How can we compare two objects of a given class to see if they are the same? Is it legal to do: Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4)

Variables, Memory and Pointers

Pointers II. Class 31

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

CSE 303: Concepts and Tools for Software Development

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to Programming using C++

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

EL2310 Scientific Programming

C++ for Java Programmers

Lecture 10: Introduction to Inheritance

Dynamic Data Structures

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Computer Programming

CSC 1052 Algorithms & Data Structures II: Linked Lists Revisited

C++11/14 Rocks. Clang Edition. Alex Korban

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

Variables and numeric types

Your first C++ program

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

pointers + memory double x; string a; int x; main overhead int y; main overhead

GEA 2017, Week 4. February 21, 2017

Intro to C: Pointers and Arrays

Launchpad Lecture -10

CSCI-1200 Data Structures Fall 2009 Lecture 25 Concurrency & Asynchronous Computing

More loops Ch

G52CPP C++ Programming Lecture 17

C++: Overview and Features

Dynamic Memory Allocation

Array Elements as Function Parameters

. p.1/23. Today. 1. Questions and discussion from lecture. 2. Type-checking Functions Arrays Records (maybe)

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CS 162, Lecture 25: Exam II Review. 30 May 2018

Lecture 9. Pointer, linked node, linked list TDDD86: DALP. Content. Contents Pointer and linked node. 1.1 Introduction. 1.

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

CS201 Some Important Definitions

Pointers, Dynamic Data, and Reference Types

Objective-C. Stanford CS193p Fall 2013

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Sudoku-4: Logical Structure

W3101: Programming Languages C++ Ramana Isukapalli

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Recap: Functions as first-class values

What does it mean by information hiding? What are the advantages of it? {5 Marks}

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Ch. 12: Operator Overloading

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

EL2310 Scientific Programming

Object Oriented Software Design II

Object Oriented Programming: Inheritance Polymorphism

COMP6771 Advanced C++ Programming

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CS427 Inheritance and Virtual Functions. Linked lists. 2/27. 01Replacement.cpp link! i n t GetY ( void ) { return ( y ) ; } ;

Computer Programming

C++ For Science and Engineering Lecture 12

Object-Oriented Programming

Object Oriented Software Design II

MIDI plugins with JUCE

Programming 2. Object Oriented Programming. Daniel POP

Introduction to Computer Science Midterm 3 Fall, Points

Computer Programming

September 10,

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

Arrays. Week 4. Assylbek Jumagaliyev

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

CS 11 C++ track: lecture 1

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Short Notes of CS201

CA31-1K DIS. Pointers. TA: You Lu

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Chapter 16. Templates. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Transcription:

Recap: Pointers IFMP 18, M. Schwerhoff int* int& *p &i &*&* ** * * * *

A 0 A 1 A 2 A 3 A 4 A 5

A 0 A 1 A 2 A 3 A 4 A 5 5 i

int* p; A 0 A 1 A 2 A 3 A 4 A 5 5 i p

int* p; p = &i; A 0 A 1 A 2 A 3 A 4 A 5 5 A 0 i p

int* p; p = &i; A 0 A 1 A 2 A 3 A 4 A 5 5 7 A 0 *p = 7; i p

int* p; p = &i; A 0 A 1 A 2 A 3 A 4 A 5 7 A 0 *p = 7; cout << i; // 7 i p

int* p; p = &i; *p = 7; cout << i; // 7 int j = *p + 1; A 0 A 1 A 2 A 3 A 4 A 5 7 A 0 8 i p j

int* p; p = &i; *p = 7; cout << i; // 7 int j = *p + 1; p = &j; A 0 A 1 A 2 A 3 A 4 A 5 7 A 04 8 i p j

int* p; p = &i; *p = 7; cout << i; // 7 int j = *p + 1; p = &j; cout << *p; // 8 A 0 A 1 A 2 A 3 A 4 A 5 7 A 4 8 i p j

int* p; p = &i; *p = 7; cout << i; // 7 int j = *p + 1; p = &j; cout << *p; // 8 (*p)++; A 0 A 1 A 2 A 3 A 4 A 5 7 A 4 8 9 i p j

int* p; p = &i; *p = 7; cout << i; // 7 int j = *p + 1; p = &j; cout << *p; // 8 (*p)++; p++; A 0 A 1 A 2 A 3 A 4 A 5 7 A 45 9 i p j

int* p = &i; A 0 A 1 A 2 A 3 A 4 A 5 5 A 0 i p

int* p = &i; A 0 A 1 A 2 A 3 A 4 A 5 cout << p; // A 0 5 A 0 i p

int* p = &i; A 0 A 1 A 2 A 3 A 4 A 5 cout << p; // A 0 5 A 0 cout << *p;// 5 i p

int* p = &i; A 0 A 1 A 2 A 3 A 4 A 5 cout << p; // A 0 5 A 0 cout << *p;// 5 cout << &p;// A 2 i p

Recap: Pointers vs. References

Pointers vs. References int* p = &i; int& r = i; Declaration and initialisation

Pointers vs. References int* p = &i; int& r = i; *p = 0; // i = 0 r = 0; // i = 0 Declaration and initialisation Writing to underlying variable

Pointers vs. References int* p = &i; int& r = i; *p = 0; // i = 0 r = 0; // i = 0 cout << *p; // 0 cout << r; // 0 Declaration and initialisation Writing to underlying variable Reading underlying variable

Pointers vs. References int* p = &i; int& r = i; *p = 0; // i = 0 r = 0; // i = 0 cout << *p; // 0 cout << r; // 0 Declaration and initialisation Writing to underlying variable Reading underlying variable int* p; int& r; p = &i; r = i; References must be initialised immediately

Pointers vs. References int* p = &i; int& r = i; *p = 0; // i = 0 r = 0; // i = 0 cout << *p; // 0 cout << r; // 0 Declaration and initialisation Writing to underlying variable Reading underlying variable int* p; int& r; p = &i; r = i; References must be initialised immediately int* p = &i; int& r = i; p = &j; r = j; References themselves cannot be changed ( redirected )

Pointers vs. References int* p = &i; int& r = i; *p = 0; // i = 0 r = 0; // i = 0 cout << *p; // 0 cout << r; // 0 Declaration and initialisation Writing to underlying variable Reading underlying variable int* p; int& r; p = &i; r = i; References must be initialised immediately int* p = &i; int& r = i; p = &j; r = j; References themselves cannot be changed (are always const) int* p = &i; int& r = i; cout << &p; cout << &r; References don t have addresses (they are just aliases of sth. else)

Why Pointers and References?

Why Pointers and References? Historical language development: C: 1969; has pointers C++: 1983 Inherited pointers from C (backwards compatibility) Added references to support operator overloading

Why Pointers and References? Historical language development: C: 1969; has pointers C++: 1983 Inherited pointers from C (backwards compatibility) Added references to support operator overloading Rule of thumb: prefer references over pointers, if possible References are more restricted harder to make mistakes Reference syntax is nicer (r vs *p) easier to read

Why Pointers and References? When do we need pointers? Dynamic memory allocation (new) Fast(er) iteration over data: e.g. arrays (last week) and containers (today)

Why Pointers and References? When do we need pointers? Dynamic memory allocation (new) Fast(er) iteration over data: e.g. arrays (last week) and containers (today) Dynamic data structures that change over time, such as lists, trees or graphs B B A C D A C D E F E F