Motivation and Overview

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 39. Copy Constructor.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
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.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
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.
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.
C++ Memory Overview 4 major memory segments Key differences from Java
Object-Oriented Programming in C++
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.
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.
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”
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.
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
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.
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: Basics. 2 Address vs. Value Each memory cell has an address associated with it
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Dynamic Allocation in C
Motivation for Generic Programming in C++
Pointers and Dynamic Arrays
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 7 Pointers and C-Strings
Overview 4 major memory segments Key differences from Java stack
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers.
Pointers and Memory Overview
Pointers and Pointer-Based Strings
Student Book An Introduction
COMP 2710 Software Construction Pointers
Dynamic Memory CSCE 121 J. Michael Moore.
Lecture 6 C++ Programming
Pointers Psst… over there.
Pointers and References
The dirty secrets of objects
Pointers Psst… over there.
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Object Oriented Programming COP3330 / CGS5409
Overview 4 major memory segments Key differences from Java stack
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers and Dynamic Variables
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 12 Pointers and Memory Management
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
CISC/CMPE320 - Prof. McLeod
9-10 Classes: A Deeper Look.
C++ Pointers and Strings
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Chapter 9: Pointers and String
Programming in C Pointers and Arrays.
Pointers and dynamic objects
COP 3330 Object-oriented Programming in C++
Pointers and References
Class rational part2.
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
9-10 Classes: A Deeper Look.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Motivation and Overview Often need to refer to another object Without making a copy of the object itself Two ways to do this Indirectly, via a pointer Gives the address of the object (analogy: street address) Requires the code to do extra work: dereferencing Like going to the given address, to talk to the person Directly, via a reference Acts as an alias for the object Code interacts with reference as if it were object itself

Review: What’s a Pointer? A variable holding an address Of what it “points to” in memory Can be untyped E.g., void * v; // points to anything However, usually they’re typed Checked by compiler Can only be assigned addresses of variables of type to which it can point E.g., int * p; // only points to int Can point to nothing E.g., p = 0; // or p = nullptr; (C++11) Can change where it points As long as pointer itself isn’t const E.g., p = &i; // now points to i int i 7 0x7fffdad0 int *p

Review: What’s a Reference? Also a variable holding an address Of what it “refers to” in memory But with a nicer interface A more direct alias for the object Hides indirection from programmers Must be typed Checked by compiler Again can only refer to the type with which it was declared E.g., int & r =i; // refers to int i Always refers to (same) something Must initialize to refer to a variable Can’t change what it aliases int i 7 0x7fffdad0 int & r

Untangling Operator Syntax Symbol Used in a declaration Used in a definition unary & (ampersand) reference, e.g., int i; int &r = i; address-of, e.g., p = & i; unary * (star) pointer, e.g., int * p; dereference, e.g., * p = 7; -> (arrow) member access via pointer, e.g., C c; C * cp=&c; cp->add(3); . (dot) member access via reference or object, e.g., C c; c.add(3); C & cr = c; cr.add(3);

Aliasing and Pointers Distinct variables have different memory locations E.g., i and j A variable and all the pointers to it (when they’re dereferenced) all alias the same location E.g., i, *p, and *q Assigning a new value to i, *p or *q changes value seen through the others But does not change value seen through j int main (int argc, char *argv[]) { int i = 0; int j = 1; int * p = & i; int * q = & i; *q = 6; // i is now 6, j is still 1 } int *p 0xefffdad0 6 int i int *q 0xefffdad0 1 int j

Review: Const Pointers Read declarations right to left Make promises via the const keyword in pointer declaration: not to change where the pointer points not to change value at the location to which it points Can (only) change Where w points and the value at the location to which it points The value at the location to which x points Where y points A pointer to non-const cannot point to a const variable neither w nor x can point to i any of them can point to j int main (int argc, char **argv) { const int i = 0; int j = 1; int k = 2; // pointer to int int * w = & j; // (array names are like const // pointers to their 0th position) // const pointer to int int * const x = & j; // pointer to const int const int * y = & i; // const pointer to const int const int * const z = & j; }

Pointers and Arrays int main (int argc, char **argv) { int arr [3] = {0, 1, 2}; int * p = & arr[0]; int * q = arr; // p, q, arr point to same place } An array holds a contiguous sequence of memory locations Can refer to locations using either array index or pointer notation E.g., *arr vs. arr[0] E.g., *(arr+1) vs. arr[1] Array variable essentially behaves like a const pointer Like int * const arr; Can’t change where it points Can change locations unless declared array-of-const E.g., const int arr[3]; Can initialize other pointers to the start of the array Using array name, or using address of 0th element int arr [3] 1 2 0xefffdad0 0xefffdad0 int *p int *q

Pointer Arithmetic With Arrays Adding or subtracting int n moves a pointer by n of the type to which it points I.e., by n array positions E.g., value in q is increased by sizeof(int) by ++q Can move either direction E.g., --q, ++p Can jump to a location E.g., p+=2, q-=1 Remember that C++ (only) guarantees that sizeof(char)==1 But compiler figures out other sizes for you int main (int argc, char **argv) { int arr [3] = {0, 1, 2}; int * p = & arr[0]; int * q = arr; // p, q now point to same place ++q; // now q points to arr[1] } int arr [3] 1 2 0xefffdad0 0xefffdad0 int *p int *q

Arrays of (and Pointers to) Pointers Can have pointers to pointers Can also have an array of pointers (like a const pointer to a pointer type) E.g., argv parameter in main Array of pointers to character strings Could also declare as a pointer to the first pointer Notice array dimension is not specified Instead a special argument (argc) holds array size By convention, character strings are zero terminated Special char is ‘\0’ not ‘0’ int main (int argc, char *argv[]) { // could declare char **argv for (int i = 0; i < argc; ++i) cout << argv[i] << endl; } t e s t \0 h e l l o \0 0xefffa0d0 0xefffaad0 char * argv[] 2 int argc

Rules for Pointer Arithmetic You can subtract (but not add, multiply, etc.) pointers Gives an integer with the distance between them You can add/subtract an integer to/from a pointer E.g., p+(q-p)/2 is allowed but (p+q)/2 gives an error Note relationship between array and pointer arithmetic Given pointer p and integer n, the expressions p[n] and *(p+n) are both allowed and mean the same thing int main (int argc, char **argv) { int arr [3] = {0, 1, 2}; int * p = & arr[0]; int * q = p + 1; return 0; } int arr [3] 1 2 0xefffdad0 0xefffdad0 int *p int *q

Watch out for Pointer Arithmetic Errors Dereferencing a 0 pointer will crash your program Accessing memory location outside your program can Crash your program Let you read arbitrary values Let you modify that location Last two: hardest to debug Watch out for Uninitialized pointers Failing to check pointer for 0 Adding or subtracting an uninitialized variable to a pointer Errors in loop initialization, termination, or increment int main (int argc, char **argv) { int arr [3] = {0, 1, 2}; int * p = & arr[0]; int * q = arr; // p, q now point to same place int n; q+=n; // now where does q point? } int arr [3] 1 2 0xefffdad0 0xefffdad0 int *p int *q

Arrays, Pointers, and Dynamic Allocation We’ve talked mainly about stack variables so far Arrays, like individual objects, can be allocated (new) and de-allocated (delete) dynamically more details in later lectures Arrays have a particular syntax with [ ] in the new and delete calls Ensures constructors and destructors of elements are called automatically Don’t leak, destroy safely Requires careful attention to details of memory management More about this in next lectures Foo * baz (){ // note the array form of new int * const a = new int [3]; a[0] = 0; a[1] = 1; a[2] = 2; Foo *f = new Foo; f->reset(a); return f; } void Foo::reset(int * array) { // ctor must initialize to 0 delete [] this->array_ptr; this->array_ptr = array; void Foo::~Foo() { // note the array form of delete

Aliasing and References int main (int argc, char **argv) { int i = 0; int j = 1; int & r = i; int & s = i; r = 8; // i is now 8, j is still 1 } An object and all the references to it alias the same location E.g., i, r, and s Assigning a new value to i, r or s changes value seen through the others But does not change value seen through j int & r 0xefffdad0 8 int i 0xefffdad0 1 int j int & s

References to Const Remember: references must refer to something Can’t be nil Also, once initialized, they cannot be changed E.g., can’t redirect t to i Const with a reference A promise not to change what’s aliased E.g., can’t use t to change j Can’t use reference to non-const with a const variable, but reverse is ok int main (int argc, char **argv) { const int i = 0; int j = 1; int & r = j; // r can’t refer to i const int & s = i; // ok for s,t const int & t = j; // to alias i,j }

Review: Parameter Passing By value Makes a copy i.e., of h into local variable a ++a does not change h By reference Alias for passed variable c = b changes j can’t change b (or i): const Can pass address by value And then use address value to change what it points to *d = c changes k ++d changes local pointer int main (int argc, char **argv) { int h = -1; int i = 0; int j = 1; int k = 2; return func (h, i, j, & k); } int func (int a, const int & b, int & c, int * d) ++a; c = b; *d = c ++d; return 0;

References to Pointers int main (int argc, char **argv) { int j = 1; int & r = j; // r aliases j int * p = & r; // p really // points to j int * & t = p; // t aliases p } Can’t have a pointer to a reference But can point to what the reference aliases Address-of operator on a reference to a variable Gives address of variable … not of reference itself Reference to a pointer An alias for the pointer … not for what it points to Useful to pass a pointer to code that may change it int & r 0xefffdad0 1 int j 0xefffdad0 int * p int * & t 0xefffdad0

Why to Use STL Vectors Instead of C++ Arrays #include <iostream> #include <vector> using namespace std; int main (int, char *[]) { vector<int> v; // This would be asking for trouble.... // v[0] = 1; v[1] = 2; v[2] = 4; // ... but this works fine... v.push_back (1); v.push_back (2); v.push_back (4); // ... and now this is ok ... for (size_t s = 0; s < v.size(); ++s) { cout << "v[" << s << "] is " << v[s] << endl; } return 0; Vectors do a lot of (often tricky) memory management for you Use new[] and delete[] internally Resize memory as needed Don’t leak memory Easier to pass to functions E.g., can tell you their size Don’t have to pass a separate size variable Don’t need a pointer by reference in order to resize Still have to pay some attention E.g., push_back can allocate more memory but operator [] can’t E.g., vectors copy and take ownership of objects they contain