1 C++ Pointers Gordon College. 2 Regular variables Regular variables declared –Memory allocated for value of specified type –Variable name associated.

Slides:



Advertisements
Similar presentations
Informática II Prof. Dr. Gustavo Patiño MJ
Advertisements

Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
February 11, 2005 More Pointers Dynamic Memory Allocation.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
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.
C++ Pointers Read Chapter 2 (P ). COP3530 – C++ Pointers Pointers Pointers provide a method of referencing the memory location of variables provide.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS 1704 Introduction to Data Structures and Software Engineering.
Review 1 List Data Structure List operations List Implementation Array Linked List.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
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.
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.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Object Lifetime and Pointers
Pointers and Dynamic Arrays
Standard Version of Starting Out with C++, 4th Edition
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9: Pointers.
Pointers.
Pointers Revisited What is variable address, name, value?
8 Pointers.
Pointers and References
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9: Pointers.
Introduction to Abstract Data Types
Chapter 9: Pointers.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
Dynamic Memory.
Standard Version of Starting Out with C++, 4th Edition
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

1 C++ Pointers Gordon College

2 Regular variables Regular variables declared –Memory allocated for value of specified type –Variable name associated with that memory location –Memory initialized with values provided (if any) 27

3 Pointers Pointer Variables –contains a memory address –pointer variable should be typed int *Ptr However could be a void pointer void *Vptr –two operators: Dereference * Address of&

4 Basic Pointer Operations Dereferencing and indirection –Pointer variable stores address of a location –Accessing contents of that location requires dereferencing operator * cout << *iPtr; int *iPtr = &i; Pointer declaration Dereferencing pointer PointerFun Video

5 Basic Pointer Operations Assignment –Pointer variables can be assigned the values of other pointer variables bound to same type int *iPtr = &i; int *jPtr = &j; jPtr = iPtr;

6 Basic Pointer Operations Consider: *jPtr = 44; –Changes value that both pointers reference –Not good programming practice, hard to debug –Known as aliasing problem

7 Basic Pointer Operations Comparison –Relational operators used to compare two pointers –Must be bound to same type –Most common = = and != –The null address may be compared with any pointer variable int* t; double *d; if (t == d) … ERROR Fix: if (t == (int*) d)

8 Initial value for pointers Variables are not automatically given an initial value by the system - they start with whatever garbage is left in memory when they are allocated. #include... Node* head = NULL; // Initialized pointer to NULL. WARNING: Invalid addresses in pointer can cause problems - known as “wild pointers” when not initialized

9 Pointers vs. References Consider this code: int i; int *p = &i; int &r = i; *p = 5; r = 5; Both p and r contain addresses which point to the variable i, however these “addressing” variables are used differently in code. Why bother with pointers?

10 Pointers vs. References Pointer arithmetic i.e. p++p-- ptr = sptr + 1; char* GetFirstT(char* p) { for ( ; *p ; ++p) { if ( *p == 't' ) return p; } return 0; } signed main() { char the_alphabet[] = "abcdefghijklmnopqrstuvwxyz"; char* p_t = GetFirstT(the_alphabet); cout << p_t - the_alphabet << endl; } OUTPUT: 19

11 Dynamic Memory Allocation The new operation Example int * intPtr; intPtr = new int; –An anonymous variable –Cannot be accessed directly –Warning: watch out for memory leak

12 Memory Management Problems Memory leak - program fails to release memory when no longer needed { int *t = new int(4); } Solution: must use the delete command before existing block Dangling Pointer - an object is deleted or deallocated, without modifying the value of the pointer. char *cp = NULL; { char c; cp = &c; } Solution: set pointer to NULL before exiting block

13 Pointer Arguments Pointers can be passed as arguments to functions This is logically equivalent to reference parameters –In fact, this is how early C++ compilers accomplished reference parameters

14 Pointer to a Pointer int cow(7); int* p_cow = &cow; int** p_p_cow(&p_cow); int*** p_p_p_cow = &p_p_cow; Name of VariableType of VariableAddress in MemoryValue Stored Cow Int p_cow int* p_p_cow int** p_p_p_cow int*** int cow(7); int* p_cow = &cow; int** p_p_cow(&p_cow); int*** p_p_p_cow = &p_p_cow; ***p_p_p_cow = 8;

15 Pointer to objects Declaration: classType *name = &object Accessing the object: (*name).method(); name->method();

16 Pointers to objects (from lab2) Pet * kennel[10];// The cages in the kennel char dashes[] = " "; kennel[0] = new Rodent("Mickey", "W. Disney", "01/01/2002", "01/04/2002","mouse"); kennel[1] = new Bird("Tweety", "Granny", " ", "9/5/2001","canary"); kennel[2] = new Reptile("Ignatius", "B. Starr", "10/07/2001", "10/10/2001","iguana"); kennel[3] = new Cat("Garfield", "J. Arbuckle", "01/01/2002", "01/31/2002",20, true); kennel[4] = new Dog("Snoopy", "C. Brown", "01/01/2002", "01/02/2002",15, false, "beagle"); kennel[5] = new Cat("Sylvester", "Granny", "09/01/2001", "09/05/2001",15, false); kennel[6] = new Dog("Butch", "Granny", "09/01/2001", "09/05/2001",45, true, "bulldog"); kennel[9] = kennel[8] = kennel[7] = NULL; for (int i = 0; i < 10 && kennel[i] != NULL; i++) { cout << endl << dashes << dashes << endl << endl; cout getName() << endl << endl; kennel[i] -> printBill(); cout << endl << dashes << dashes << endl; }