Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
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.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
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 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Intro to C++ for Robotics Made for ALARM. Computer System Hardware – cpu, memory, i/o Software – OS, drivers.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC.
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.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Data Types Declarations Expressions Data storage C++ Basics.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Meet with your team later today in ELL321 at 6:30pm. Teams are listed on the Project page of the course.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (23 left – as of last night). Are you able.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). SDD document framework should be set up in your Wiki by now.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
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.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Pointers What is the data type of pointer variables?
LESSON 06.
Motivation and Overview
CISC/CMPE320 - Prof. McLeod
Precedence and Associativity
Pointers Psst… over there.
CISC/CMPE320 - Prof. McLeod
Pointers Psst… over there.
All the Operators 22-Nov-18.
All the Operators 4-Dec-18.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
All the Operators 6-Apr-19.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
All the Operators 13-Apr-19.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours (all courses) added to course web site: –Tuesdays 4:30 to 5:30 –Wednesdays 4:30 to 5:30 –Thursdays 12:30 to 2:15 Meet with your team this week. Teams will be posted as soon as I can get it done – before Thursday. I will tell you what you will be doing in Thursday’s lecture.

Today Introduction to References and Pointers Arrays Structs Start on Operators Fall 2015CISC/CMPE320 - Prof. McLeod2

References and Pointers, First Pass… Pointers are not references and references are not pointers! A reference is an “alias for its initializer”. [From “C++ Gotchas” by Stephen C. Dewhurst] The alias must refer to something. You cannot have a null reference or a reference to void or even a reference to a reference. Fall 2015CISC/CMPE320 - Prof. McLeod3

Pointers A pointer is a variable that stores a memory address. It occupies memory (a reference may not occupy any memory). If the memory address was obtained using the & operator (“address of”) on another variable, then the pointer indirectly references the value contained by the other variable. Accessing this value through the pointer (using de-referencing) is called indirection. Fall 2015CISC/CMPE320 - Prof. McLeod4

Fall 2015CISC/CMPE320 - Prof. McLeod5 Pointers, Cont. In Java, pointers are implicit – you have no control over them. In C and C++ pointers are explicit, you have complete control! Pointers can be null. You can have such a thing as void* as a pointer type.

& and * Operators When creating a type, & gives you a reference to a type, * gives you a pointer to a variable of that type. In an expression, & is the “address of” operator yielding the memory address of a variable. * is “de-referencing”. When used with a pointer it yields the value of the variable being pointed to. Fall 2015CISC/CMPE320 - Prof. McLeod6

References and Pointers, Cont. This gets even more interesting when moving things in and out of functions. Later… Pointers tend to be overused in C++ code – try to use a reference first. See TestSimplePointers.cpp Fall 2015CISC/CMPE320 - Prof. McLeod7

Fall 2015CISC/CMPE320 - Prof. McLeod8 Pointers, Review Questions I can have many pointers aliased to a fundamental type variable, does this mean that an int is an Object, for example? What is stored in a pointer? What does it mean to “de-reference” a pointer, and how do you do it? If I add one to a pointer (pointer arithmetic), then how many bytes are added to the memory address and how does the system know?

Fall 2015CISC/CMPE320 - Prof. McLeod9 Pointers, Review Questions Cont. I can use pointer arithmetic to access memory positions away from the original pointer: –Can I access any memory position I want? –Can I access and change any memory position I want? –Is this a good idea? How much memory does a pointer occupy? How do you get the address of where the pointer is stored? Am I using 32 bit pointers on a 64 bit machine? Why?

Fall 2015CISC/CMPE320 - Prof. McLeod10 Aside – Declaring Multiple Variables This is when you do something like: int x, y, z; But this does not work with operators. For example: int* x, y; x is a pointer, but y is an int

Fall 2015CISC/CMPE320 - Prof. McLeod11 Aside - typedef Keyword If you prefix a type with the typedef keyword, then you can create a synonym for the type. For example: typedef long double dprecision; Now, you can use dprecision instead of using long double for a type.

Fall 2015CISC/CMPE320 - Prof. McLeod12 Arrays To declare a variable to hold five integers: int anArray[5]; Or you can use an array initializer: int anArray[] = {2, 7, 3, 0, 1}; or int anArray[5] = {2, 7, 3, 0, 1};

Fall 2015CISC/CMPE320 - Prof. McLeod13 Arrays, Cont. What is in an uninitialized array? Can I use pointer arithmetic with an array? Can I access values outside the array bounds? See ArrayExample.cpp vector s are much better to use than arrays – more about this class later…

Fall 2015CISC/CMPE320 - Prof. McLeod14 Arrays and Strings In C, you had to use char[] to store a string literal: char oldString[] = "Hello there!"; These “C-strings” end with the character '\0', also called the “null character”. Manipulating C-strings means manipulating arrays – generally a real pain… In C++ use the string class instead! Defined in the string library, and uses the std namespace. More about string s later…

Fall 2015CISC/CMPE320 - Prof. McLeod15 Structures A kind of class. Defined as an aggregate of other types. For example: struct address { string name; int streetNumber; string street; string city; string province; string postalCode; }; Note “;”

Fall 2015CISC/CMPE320 - Prof. McLeod16 Structures, Cont. See StructureDemo.cpp (Also note use of pointers with a function, and use of const in parameter list – getting a bit ahead of ourselves!!) Note how Eclipse gives you the membership list for an address.

Fall 2015CISC/CMPE320 - Prof. McLeod17 Member Selection Operators The example also demonstrated the use of the two member selection operators: The “dot operator”: object.member De-referencing and membership: pointer->member The latter is the same as: (*pointer).member (“Members” are attributes or methods (or member functions)…)

Structures, Cont. A struct is a primitive object definition with no privacy for it’s members. Why use them in C++? Fall 2015CISC/CMPE320 - Prof. McLeod18

Fall 2015CISC/CMPE320 - Prof. McLeod19 Operators Discussed in order of highest to lowest precedence. Highest Precedence: Scope resolution (when defining only): class_name::member Or namespace_name::member

Fall 2015CISC/CMPE320 - Prof. McLeod20 Operators, Cont. Next Highest Precedence (level 2): Member selection. (see slide 17). Subscripting arrays: pointer[expr] ( ) when used with function calls or value constructors. Post increment or post decrement: var++ var—- Type identification: typeid(type or expr) Casts like static_cast (expr)

Fall 2015CISC/CMPE320 - Prof. McLeod21 Aside – typeid() Used to find out the types of pointers. More useful in a function: A trivial example: int aVal = 100; int* ptr_aVal = &aVal; cout << typeid(ptr_aVal).name() << endl; Displays: Pi

Fall 2015CISC/CMPE320 - Prof. McLeod22 Operators, Cont. Level 3 Precedence: sizeof(type) Pre-increment and pre-decrement. Complement: ~expr Not: !expr Negation: -expr Address of and dereference ( & and * ) The heap operators: new, delete and delete[] C – style cast: (type)expr

Fall 2015CISC/CMPE320 - Prof. McLeod23 Operators, Cont. Level 4: Member selection (and dereference) -> Level 5: Multiply, Divide, Modulo: *, /, % Level 6: Add, subtract: +, -

Fall 2015CISC/CMPE320 - Prof. McLeod24 Operators, Cont. Level 7: Bitwise shift left: expr << expr Bitwise shift right: expr >> expr Level 8:, >= Level 9: ==, !=

Fall 2015CISC/CMPE320 - Prof. McLeod25 Operators, Cont. Levels 10 to 12: Bitwise AND: expr & expr Bitwise exclusive OR (or XOR): expr ^ expr Bitwise OR: expr | expr Levels 13 and 14: Logical AND: expr && expr Logical OR: expr || expr Level 15: Conditional Expression: expr ? expr : expr

Fall 2015CISC/CMPE320 - Prof. McLeod26 Operators, Cont. (Still!) Level 16: All assignment operators: = *=, /=, %=, +=, -=, >=, &=, |=, ^= Level 17: Throw exception: throw expr Lowest Precedence!: Comma:,

Fall 2015CISC/CMPE320 - Prof. McLeod27 Notes on Precedence Prefix operators ( ~, !, -, &, *, pre-increment and pre-decrement) and assignment operators are right associative (right to left) – all others are left- associative (left to right). So: int a = b + c + d; Means int a = (b + c) + d; But, int x = y = z; Means int x = (y = z); (Yes, an assignment operator can return something!)

Fall 2015CISC/CMPE320 - Prof. McLeod28 Precedence Notes, Cont. Use ( ) to control precedence. When in doubt, control precedence! If an expression gets too long, use intermediate variables to make it more readable.