Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.

Slides:



Advertisements
Similar presentations
Class and Objects.
Advertisements

Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
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.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compilation time * Dynamic Memory.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Pointers and Dynamic Objects Mechanisms for developing flexible list representations.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Classes: A Deeper Look Systems Programming.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Dynamic Objects II. COMP104 Lecture 31 / Slide 2 A Simple Dynamic List  An integer list: IntArray * Features n Can be passed by value & reference n Can.
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Pointer Data Type and Pointer Variables
Templates and Polymorphism Generic functions and classes
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Review 1 List Data Structure List operations List Implementation Array Linked List.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Chapter 11 Mechanisms for developing flexible list representations Pointers and dynamic memory.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Classes with Dynamically Allocated Data
Pointers and dynamic objects
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers.
COMS 261 Computer Science I
Dynamic Objects.
CS410 – Software Engineering Lecture #5: C++ Basics III
Pointers and dynamic objects
Dynamic Objects.
Presentation transcript:

Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.

Usefulness Mechanism in C++ to pass command-line parameters to a program This feature is less important now with the use of graphical interfaces Mechanism in C++ to pass command-line parameters to a program This feature is less important now with the use of graphical interfaces Necessary for dynamic objects Objects whose memory is acquired during program execution as the result of a specific program request  Dynamic objects can survive the execution of the function in which they are acquired Dynamic objects enable variable-sized lists

Categorizing Expressions Lvalue expressions Represent objects that can be evaluated and modified Rvalue expressions Represent objects that can only be evaluated Consider int a; vector b(3); int c[3]; a = 1; // a: lvalue c[0] = 2*a + b[0]; // c[0], a, b[0]: lvalues Observation Not all lvalues are the names of objects

Basics Pointer Object whose value represents the location of another object In C++ there are pointer types for each type of object  Pointers to int objects  Pointers to char objects  Pointers to RectangleShape objects Even pointers to pointers  Pointers to pointers to int objects

Examples of uninitialized pointers int *iPtr; // iPtr is a pointer to an int char *s; // s is a pointer to a char Rational *rPtr; // rPtr is a pointer to a // Rational Examples of initialized pointers int i = 1; char c = 'y'; int *ptr = &i; // ptr is a pointer to int i char *t = &c; // t is a pointer to a char c Indicates pointer object Indicates to take the address of the object Syntax Examples of uninitialized pointers int *iPtr; // iPtr is a pointer to an int char *s; // s is a pointer to a char Rational *rPtr; // rPtr is a pointer to a // Rational Indicates pointer object

Memory Depiction int i = 1; char c = 'y'; int *ptr = &i; char *t = &c

Indirection Operator An asterisk has two uses with regard to pointers In a definition, it indicates that the object is a pointer char *s; // s is of type pointer to char In expressions, when applied to a pointer it evaluates to the object to which the pointer points int i = 1; int *ptr = &i; // ptr points to i *ptr = 2; cout << i << endl; // display a 2 * indicates indirection or dereferencing *ptr is an lvalue

Address Operator & use is not limited to definition initialization int i = 1; int j = 2; int *ptr; ptr = &i; // ptr points to location of i *ptr = 3; // contents of i are updated ptr = &j; // ptr points to location of j *ptr = 4; // contents of j are updated cout << i << " " << j << endl;

Null Address 0 is a pointer constant that represents the empty or null address Its value indicates that pointer is not pointing to a valid object Cannot dereference a pointer whose value is null int *ptr = 0; cout << *ptr << endl; // invalid, ptr // does not point to // a valid int

Member Indirection Consider Rational r(4,3); Rational rPtr = &r; To select a member of r using rPtr and member selection, operator precedence requires (*rPtr).Insert(cout); Invokes member Insert() of the object to which rPtr points (r) Consider Rational r(4,3); Rational rPtr = &r; To select a member of r using rPtr and member selection, operator precedence requires (*rPtr).Insert(cout); This syntax is clumsy, so C++ provides the indirect member selector operator -> rPtr->Insert(cout); Invokes member Insert() of the object to which rPtr points (r)

Traditional Pointer Usage void IndirectSwap(char *Ptr1, char *Ptr2) { char c = *Ptr1; *Ptr1 = *Ptr2; *Ptr2 = c; } int main() { char a = 'y'; char b = 'n'; IndirectSwap(&a, &b); cout << a << b << endl; return 0; } In C, there are no reference parameters. Pointers are used to simulate them.

Constants and Pointers A constant pointer is a pointer such that we cannot change the location to which the pointer points char c = 'c'; const char d = 'd'; char * const ptr1 = &c; ptr1 = &d; // illegal A constant pointer is a pointer such that we cannot change the location to which the pointer points char c = 'c'; const char d = 'd'; char * const ptr1 = &c; ptr1 = &d; // illegal A pointer to a constant value is a pointer object such that the value at the location to which the pointer points is considered constant const char *ptr2 = &d; *ptr2 = 'e'; // illegal: cannot change d // through indirection with ptr2

Local objects and parameters Object memory is acquired automatically Object memory is returned automatically when object goes out of scope Dynamic objects Object memory is acquired by program with an allocation request  new operation Dynamic objects can exist beyond the function in which they were allocated Object memory is returned by a deallocation request  delete operation Local objects and parameters Object memory is acquired automatically Dynamic objects Object memory is acquired by program with an allocation request  new operation Differences Local objects and parameters Dynamic object

Operation specifies The type and number of objects If there is sufficient memory to satisfy the request A pointer to sufficient memory is returned by the operation If there is insufficient memory to satisfy the request An exception is generated  An exception is an error state/condition which if not handled (corrected) causes the program to terminate General New Operation Behavior Memory for dynamic objects Requested from the free store  Free store is memory controlled by operating system

The Basic New Form Syntax Ptr = new SomeType ; Where  Ptr is a pointer of type SomeType Beware The newly acquired memory is uninitialized unless there is a default SomeType constructor

Examples int *iptr = new int; Rational *rptr = new Rational;

Another Basic New Form Syntax SomeType *Ptr = new SomeType(ParameterList); Where  Ptr is a pointer of type SomeType Initialization The newly acquired memory is initialized using a SomeType constructor ParameterList provides the parameters to the constructor

Examples int *iptr = new int(10); Rational *rptr = new Rational(1,2);

The Primary New Form Syntax P = new SomeType [Expression] ; Where  P is a pointer of type SomeType  Expression is the number of contiguous objects of type SomeType to be constructed -- we are making a list Note  The newly acquired list is initialized if there is a default SomeType constructor Because of flexible pointer syntax P can be considered to be an array

Examples int *A = new int [3]; Rational *R = new Rational[2]; A[1] = 5; Rational r(2/3); R[0] = r;

Right Array For The Job cout << "Enter list size: "; int n; cin >> n; int *A = new int[n]; GetList(A, n); SelectionSort(A, n); DisplayList(A, n); Note Use of the container classes of the STL is preferred from a software engineering viewpoint  Example vector class

Delete Operators Forms of request delete P; // used if storage came from new delete [] P; // used if storage came from new[] Storage pointed to by P is returned to free store  P is now undefined

Cleaning Up int n; cout << "Enter list size: "; cin >> n; int *A = new int[n]; GetList(A, n); SelectionSort(A, n); DisplayList(A, n); delete [] A;

Dangling Pointer Pitfall int *A = new int[5]; for (int i = 0; i < 5; ++i) A[i] = i; int *B = A; delete [] A;

Memory Leak Pitfall int *A = new int [5]; for (int i = 0; i < 5; ++i) A[i] = i; A = new int [5];

A Simple Dynamic List Type What we want An integer list data type IntList with the basic features of the vector data type from the Standard Template Library Features and abilities True object  Can be passed by value and reference  Can be assigned and copied Inspect and mutate individual elements Inspect list size Resize list Insert and extract a list

Sample IntList Usage IntList A(5, 1); IntList B(10, 2); IntList C(5, 4); for (int i = 0, i < A.size(); ++i) { A[i] = C[i]; } cout << A << endl; // [ ] A = B; A[1] = 5; cout << A << endl; // [ ]

IntList Definition class IntList { public: // constructors IntList(int n = 10, int val = 0); IntList(const int A[], int n); IntList(const IntList &A); // destructor ~IntList(); // inspector for size of the list int size() const; // assignment operator IntList & operator=(const IntList &A);

IntList Definition (continued) public: // inspector for element of constant list const int& operator[](int i) const; // inspector/mutator for element of // nonconstant list int& operator[](int i); // resize list void resize(int n = 0, int val = 0); // convenience for adding new last element void push_back(int val);

IntList Definition (continued) private: // data members int *Values; // pointer to elements int NumberValues; // size of list }; // IntList auxiliary operators -- nonmembers ostream& operator<<(ostream &sout, const IntList &A); istream& operator>>(istream &sin, IntList &A);

Default Constructor IntList::IntList(int n, int val) { assert(n > 0); NumberValues = n; Values = new int [n]; assert(Values); for (int i = 0; i < n; ++i) { Values[i] = val; }

Gang of Three Rule If a class has a data member that points to dynamic memory then that class normally needs a class-defined Copy constructor  Constructor that builds an object out of an object of the same type Member assignment operator  Resets an object using another object of the same type as a basis Destructor  Anti-constructor that typically uses delete the operator on the data members that point to dynamic memory

Why A Tailored Copy Constructor Suppose we use the default copy constructor IntList A(3, 1); IntList B(A); And then A[2] = 2; Then B[2] is changed! Not what a client would expect Implication Must use tailored copy constructor

Tailored Copy Constructor IntList::IntList(const IntList &A) { NumberValues = A.size(); Values = new int [size()]; assert(Values); for (int i = 0; i < size(); ++i) Values[i] = A[i]; } What kind of subscripting is being performed?

Gang Of Three What happens when an IntList goes out of scope? If there is nothing planned, then we would have a memory leak Need to have the dynamic memory automatically deleted Define a destructor  A class object going out of scope automatically has its destructor invoked IntList::~IntList() { delete [] Values; } Notice the tilde

First Assignment Attempt Algorithm Return existing dynamic memory Acquire sufficient new dynamic memory Copy the size and the elements of the source object to the target element

Initial Implementation (Wrong) IntList& operator=(const IntList &A) { NumberValues = A.size(); delete [] Values; Values = new int [NumberValues ]; assert(Values); for (int i = 0; i < A.size(); ++i) Values[i] = A[i]; return A; } Consider what happens with the code segment IntList C(5,1); C = C;

This Pointer Consider this Inside a member function or member operator this is a pointer to the invoking object IntList::size() { return NumberValues; } or equivalently IntList::size() { return this->NumberValues; }

Member Assignment Operator IntList& IntList::operator=(const IntList &A) { if (this != &A) { delete [] Values; NumberValues = A.size(); Values = new int [A.size()]; assert(Values); for (int i = 0; i < A.size(); ++i) { Values[i] = A[i]; } return *this; } Notice the different uses of the subscript operator Why the asterisk?

Accessing List Elements // Compute an rvalue (access constant element) const int& IntList::operator[](int i) const { assert((i >= 0) && (i < size())); return Values[i]; } // Compute an lvalue int& IntList::operator[](int i) { assert((i >= 0) && (i < size())); return Values[i]; }

Stream Operators Should they be members? class IntList { //... ostream& operator<<(ostream &sout); //... }; Answer is based on the form we want the operation to take IntList A(5,1); A << cout; // member form (unnatural) cout << A; // nonmember form (natural)

Beware of Friends If a class needs to Can provide complete access rights to a nonmember function, operator, or even another class  Called a friend Declaration example class IntList { //... friend ostream& operator<< ( ostream &sout, const IntList &A); //... };

Implementing Friend << ostream& operator<<(ostream &sout, const IntList &A){ sout << "[ "; for (int i = 0; i < A.NumberValues; ++i) { sout << A.Values[i] << " "; } sout << "]"; return sout; } Is there any need for this friendship?

Proper << Implementation ostream& operator<<(ostream &sout, const IntList &A){ sout << "[ "; for (int i = 0; i < A.size(); ++i) { sout << A[i] << " "; } sout << "]"; return sout; }