CISC/CMPE320 - Prof. McLeod

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

Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
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.
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.
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.
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.
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
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 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.
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.
Data types Data types Basic types
Motivation and Overview
Pointers.
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155: Meet your TA.
CISC/CMPE320 - Prof. McLeod
Precedence and Associativity
CISC/CMPE320 - Prof. McLeod
8 Pointers.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Pointer Basics Psst… over there.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers and Dynamic Variables
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
Chapter 15 Pointers, Dynamic Data, and Reference Types
Memory, Data, & Addressing II CSE 351 Winter 2018
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
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:
CISC124 TA names and s have been added to the course web site.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Memory, Data, & Addressing II CSE 351 Winter 2018
Overloading functions
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Herbert G. Mayer, PSU CS Status 7/19/2015
CMPE212 – Reminders Course Web Site:
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Chapter 9: Pointers and String
Pointer Basics Psst… over there.
CISC/CMPE320 - Prof. McLeod
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CISC/CMPE320 - Prof. McLeod Winter 2013 CISC/CMPE320 5/9/2019 CISC/CMPE320 TA names and contact information have been added to the Instructor page of the course web site. Last reminder: Please fill out the “Programming and Teamwork Background” survey in onQ (130 out of 139 so far). Survey will close today, 7pm. No tutorial today – it will start next week. You will then know your assigned team and who your mentor is. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

CISC/CMPE320 - Prof. McLeod Today C++ Types and their behaviour, Cont.: Back to: References and Pointers Arrays and C-strings Structs Passing “things” Membership Operators (if we have time) Fall 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Pointers A pointer is a variable that stores a memory address. It occupies memory (a reference may not occupy any memory). (How much 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 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 (or better yet, use nullptr) C++11 Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod & and * Operators LHS: When creating a type, & gives you a reference to a type, * gives you a pointer to a variable of that type. RHS: 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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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 Also, auto does not work with multiple declarations. Fall 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 vectors are much better to use than arrays – more about this class later… Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 strings later… Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Winter 2014 CISC/CMPE320 5/9/2019 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 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

CISC/CMPE320 - Prof. McLeod Structures, Cont. See StructureDemo.cpp Demonstrate some other stuff too!: Passing by value. Passing by reference. Passing by constant reference. Passing a pointer. Passing a pointer to a constant. Passing a constant pointer. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Passing… Does it make any sense to pass by constant value? Do you understand the difference between a “pointer to const” and a “const pointer”? It does matter where you position the const when typing a pointer. The best technique to use, if you can, is to pass by constant reference. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Lots of consts: const int* ptr ptr points to an int* variable that is constant. int* const ptr ptr is a constant pointing to an int* variable that is not constant. const int* const ptr ptr is a constant pointing to an int* variable that is also constant. Fall 2018 CISC/CMPE320 - Prof. McLeod

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)…) Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Structures, Cont. A struct is a primitive object definition with no privacy for it’s members. Why use them in C++? (It was great to use the struct in this demo because it behaved like a very simple class!) Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operators Discussed in order of highest to lowest precedence. See: http://en.cppreference.com/w/cpp/language/operator_precedence Highest Precedence: Scope resolution (when defining only): class_name::member Or namespace_name::member Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operators, Cont. Next Highest Precedence (level 2): Member selection . (see slide 19). 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<type>(expr) Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod

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

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

CISC/CMPE320 - Prof. McLeod 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 AND has precedence over OR Fall 2018 CISC/CMPE320 - Prof. McLeod

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

CISC/CMPE320 - Prof. McLeod 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, the assignment operator returns something!) Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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. Fall 2018 CISC/CMPE320 - Prof. McLeod