Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

An introduction to pointers in c
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Template class Wrapper { public: T* operator->() { return &myT; } private: T myT; }; int main() { Wrapper wThing; wThing- >Foo(); // calls Thing::Foo()...
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
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.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
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.
Module 2: User Data types #1 2000/01Scientific Computing in OOCourse code 3C59 Module 2: User defined Data Types & Operations upon them In this module.
Object Oriented Programming Philosophy. Part 1 -- Basic Understanding & Encapsulation.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
Module 10: Simple Inheritance #1 2000/01Scientific Computing in OOCourse code 3C59 Module 10: Simple Inheritance In this module we will cover Inheritance.
Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Objects Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Pointers OVERVIEW.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Object-Oriented Programming in C++
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Module 8: more on methods #1 2000/01Scientific Computing in OOCourse code 3C59 Module 8: More on methods: In this module we will cover: Overloading of.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
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:
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Module : Technical topics #1 2000/01Scientific Computing in OOCourse code 3C59 Module : Technical topics In this module we have grouped together several.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
C++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
1 Introduction to Object Oriented Programming Chapter 10.
Module 7: Constructors #1 2000/2001Scientific Computing in OOCourse code 3C59 Module 7: Constructors and Destructors: In this module we will cover: Constructors.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Module 11: Polymorhism #1 2000/01Scientific Computing in OOCourse code 3C59 Module 11: Polymorphism and virtual methods In this module we will cover Polymorphism.
User-Written Functions
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Dynamic Memory CSCE 121 J. Michael Moore.
User-Defined Functions
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 15 Pointers, Dynamic Data, and Reference Types
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Classes and Objects.
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
References Revisted (Ch 5)
Dynamic Memory CSCE 121.
Presentation transcript:

Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives and objects Argument passing using pointers Memory allocation using new Memory freeing using delete An important use of a destructor You will probably be confused by the end of this. Particularly on the relation between pointers and references

Technical Module : Pointers #2 2000/01Scientific Computing in OOCourse code 3C59 Aims of this module In this module we teach you about the use of pointers. It will at first seem that pointers are the same as references, just more obtuse to use. In many cases this is true, but there are situations where you need to use pointers. In this module we first explain pointers and then show them in use for dynamic memory allocation.

Technical Module : Pointers #3 2000/01Scientific Computing in OOCourse code 3C59 1 Pointers Pointers were invented as part of C long before references were ever invented in C++ Pointers, as the name suggests, allow you to indirectly refer to a primitive or object. References are in many cases a simpler and more obvious way to indirectly refer to a primitive or object. Therefore in many situations you no longer need pointers where they would have been used in C. However pointers pervade C++ programs, so we must learn about them. [ You might like to note that pointers have been abolished in Java. Only "references" exist, but confusingly they are similar to but not the same as references in C++. ]

Technical Module : Pointers #4 2000/01Scientific Computing in OOCourse code 3C59 In its simplest form a pointer is declared and used like this // Declare a pointer int* ptrToInt ; // Use a pointer *ptrToInt = 3 ; ptrToInt is NOT an integer It is a thing which can POINT to an integer ptrToInt is "dereferenced" using the * operator. This line says: "set the thing which ptrToInt points to to be 3"

Technical Module : Pointers #5 2000/01Scientific Computing in OOCourse code 3C59 Now we get confusing: here is how you set a pointer to point to an existing variable: // Declare a pointer int count = 5 ; int* ptrToInt ; // Set pointer to point to count ptrToInt &count ; This line says: "set ptrToInt points to be the address of count" It is the & operator which "takes the address of" count Annoyingly and very confusingly, this use of & has absolutely nothing to do with the use of & in the context of references ! You WILL BE confused !

Technical Module : Pointers #6 2000/01Scientific Computing in OOCourse code 3C59 Pictorial examples: Pointer to an integer: *&$$%^ %$£$^^ int a ; a is an integer - space has been allocated and probably contains garbage It works the other way around, i.e. int b ; b = *pa ; int* pa ; pa is a pointer to an integer, but doesn’t point to anything sensible yet *&$$%^ %$£$^^ pa = &a ; pa now points to a, remember & means “take address of”. 10 *pa = 10 ; This places value 10 in the thing pointed to

Technical Module : Pointers #7 2000/01Scientific Computing in OOCourse code 3C59 2 Pointers to objects In the same way (with one minor additional complication) one may use pointers to objects like this: // Make a Thing object Thing wombat ; // Make a pointer to a Thing Thing* ptrToAThing ; // Now set the pointer to point at wombat ptrToAthing = &wombat ; Wombat

Technical Module : Pointers #8 2000/01Scientific Computing in OOCourse code 3C59 The minor complication is that when you have a pointer to an object you normally use the -> operator to invoke its methods Instead of this (which works fine) // make a pointer to a thing Thing* ptrToAThing ; //.... set it to point at some sensible Thing object... // Now use the printContents method in an "obvious" fashion (*ptrToAthing).printContents() you normally do this: // make a pointer to a thing Thing* ptrToAThing ; //.... set it to point at some sensible Thing object... // Now use the printContents method using -> operator ptrToAthing->printContents()

Technical Module : Pointers #9 2000/01Scientific Computing in OOCourse code 3C59 At this points students normally ask why ? what is the point of pointers ? what do they do that references dont do ? As we said earleir, in many cases the answer is nothing because pointers were invented in C before C++ references. If we tried to cover every way of using pointers in abstact examples now it would undoubtedly confuse you more than the good it would do. (you would be unique if this were not the case !) We therefore cover only two very common uses now: argument passing dynamic memory allocation and defer any other use of pointers to well defined situations where they might arise, and where hopefuly the purpose of use wouldbe clear.

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 Before C++ references were invented, pointers HAD to be used in C to pass arguments which you wanted to change. You can still do this in C++ like this: This passes the address of count into to doSomething int count ; doSomething( &count ) ; count This makes an area in memory labeled as count doSomething then uses the pointer to work on the original 3 Using pointers to pass primitive arguments

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 Inside the function doSomething you have to make the arguments match this void doSomething( int* pcount ) { // accessing the integer pointed // to by pcount *pcount = *pcount + 1 ; … rest of code } This is how you tell the function that it is receiving a pointer to the argument, I.e. a “pointer to an integer” rather than “an integer itself” This is how you access the thing pointed to

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 This is identical to passing primitives (it can be used instead of passing a reference, but there is generally no point in doing so) This passes the address of t into to doSomething Thing t ; doSomething( &t ) ; t This makes an area in memory labeled as t doSomething then uses the pointer to work on the original 4 Using pointers to pass objects

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 If you are passing a user defined data type you probably want to use its methods. You normally do that like this: void doSomething( Thing* ptr ) { // accessing a method // through the pointer ptr->initialise( 10.) ;... rest of code } // Declare user defined data type Thing t ; // Call a function to change it: doSomething( & t ) ; This is how you access methods of the thing pointed to.

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 So far the only way we have ever created instances of data types is by declaring them in a piece of code An alternative in C++ to explicitly create things in a free area of memory using new 5 Memory allocation using "new"

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 /* Example of use of new */ ThreeVector* vec ; vec = new ThreeVector ; vec is declared as a pointer to a ThreeVector (but at this stage it doesnt point to anything - it is un- initialised). At this point an area of memory is carved off and protected. vec is then set to point to it Memory for a ThreeVector Think of new as a function which returns a pointer to a new instance of the data type specified after it

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 As an example of where you might use pointers with new, here is a a puzzle for the whiteboard Which of the following works/doesn’t work ? Explain why ?

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 // Code A main() { ThreeVector vec ; vec = gimmeAVector( ) ; dump( vec ) ; } // Code A ThreeVector gimmeAVector() { ThreeVector v ; return v ; }

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 // Code B main() { ThreeVector* pvec ; pvec = gimmeAVector( ) ; dump2( pvec ) ; } // Code B ThreeVector* gimmeAVector() { ThreeVector* pv ; return pv ; }

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 // Code C main() { ThreeVector* pvec ; pvec = gimmeAVector( ) ; dump( pvec ) ; } // Code C ThreeVector* gimmeAVector() { ThreeVector v ; return &v ; }

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 // Code D main() { ThreeVector* pvec ; pvec = gimmeAVector( ) ; dump( pvec ) ; } // Code D ThreeVector* gimmeAVector() { ThreeVector* pv ; pv = new ThreeVector ; return pv ; }

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 // Code D ThreeVector* gimmeAVector() { ThreeVector* pv ; pv = new ThreeVector ; return pv ; } Very typically pointers are used like this, i.e. -when you want to create something long lived inside a function - when you want to return this thing to the caller without it going out of scope when you terminate

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 // Example of use of new and delete // in a function void myFunction { ThreeVector* pvec ; pvec = new ThreeVector ;.... delete pvec ; return; } Very important ! When you have finished with memory you must delete it in order to give the memory back to the system Memory for a ThreeVector 6 Memory de-allocation using "delete"

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 // Example of use of new and delete // in a function void myFunction { ThreeVector* pvec ; pvec = new ThreeVector ;.... // forgot to delete( pvec ) ; return; } If you fail to do this you will cause a "memory leak" The computer memory gets filled up with orphaned memory, and eventually falls over. Memory for a ThreeVector

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 The function returns, and the pointer goes out of scope. But the memory remains allocated with nothing pointing to it ! Memory for a ThreeVector

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 The most common situation where a memory leak occurs arises when you allocate some memory using new within an object, then forget to delete it before the the object gets destroyed 7 An important use of the destructor

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 Here is an example of the sort of problem which can arise. // The.h file for a badly written class class BadClass { private: Thing* ptr ; public: BadClass( ) ; } // The.cc file with constructor BadClass::BadClass( ) { ptr = new Thing ; }

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 When you make a BadClass, then (i) memory for a BadClass is allocated (ii) the BadClass constructor is called which uses new to allocate memory for a Thing, and sets the pointer to it // some piece of code which makes a // BadClass instance void mycode() { BadClass b ;... use it... return ; } BadClass

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 When you make a BadClass, then (i) memory for a BadClass is allocated (ii) the BadClass constructor is called which uses new to allocate memory for a Thing, and sets the pointer to it Thing // some piece of code which makes a // BadClass instance void mycode() { BadClass b ;... use it... return ; } BadClass

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 Thing // some piece of code which makes a // BadClass instance void mycode() { BadClass b ;... use it... return ; } BadClass When a BadClass object goes out of scope, then it gets destroyed, but this only deletes the BadClass itself The Thing is left hanging around with nothing pointing to it This is a MEMORY LEAK

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 Thing // some piece of code which makes a // BadClass instance void mycode() { BadClass b ;... use it... return ; } When a BadClass object goes out of scope, then it gets destroyed, but this only deletes the BadClass itself The Thing is left hanging around with nothing pointing to it This is a MEMORY LEAK

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 You should instead supply a destructor ~BetterClass( ) This destructor gets called immediately before a BetterClass object gets destroyed // The.h file for // A beter written class class BetterClass { private: Thing* ptr ; public: BetterClass( ) ; ~BetterClass( ) ; } // In the.cc file BetterClass::~BetterClass( ) { delete ptr ; } It deletes the Thing explicitly before the BetterClass object goes out of existence

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 When you make a BetterClass, then (i) memory for a BetterClass is allocated (ii) the BetterClass constructor is called which uses new to allocate memory for a Thing, and sets the pointer to it // some piece of code which makes a // BetterClass instance void mycode() { BetterClass b;... use it... return ; } Better Class

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 When you make a BetterClass, then (i) memory for a BetterClass is allocated (ii) the BetterClass constructor is called which uses new to allocate memory for a Thing, and sets the pointer to it Thing // some piece of code which makes a // BetterClass instance void mycode() { BetterClass b;... use it... return ; } Better Class

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 Thing // some piece of code which makes a // BadClass instance void mycode() { BetterClass b ;... use it... return ; } Better Class When a BetterClass object goes out of scope, (i) the destructor is called first (ii) then it gets destroyed

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 // some piece of code which makes a // BadClass instance void mycode() { BetterClass b ;... use it... return ; } Better Class When a BetterClass object goes out of scope, (i) the destructor is called first (ii) then it gets destroyed

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 // some piece of code which makes a // BadClass instance void mycode() { BetterClass b ;... use it... return ; } When a BetterClass object goes out of scope, (i) the destructor is called first (ii) then it gets destroyed

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 Be careful You only need to do this if your class is the only thing which points to some memory from inside itself. You don’t need to do this is you are simply storing a pointer to something passed into you from outside via a method. For in such a case the outside environment is by definition keeping track of the memory in question. We repeat from an earlier module: You rarely need a destructor - although it is considered good practice to always provide one even if it does nothing. However always consider it if you have used the new(..) operator inside your class Memory leaks are one pf the biggest problems of C++ (JAVA for instance does not have this problem)

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 8 this-> C++ provides a special pointer Sometimes an object has need to refer to itself. Examples are: to pass itself to a function it is calling to pass a pointer to itself to a function to return a copy of itself, or a pointer to itself. C++ provides a special inbuilt pointer in every object: this-> this-> is actually very simple once you have grasped it.

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 // To explicitly refer to yourself inside a method ThreeVector::magnitude( ) { float magsq = this->x * this->x + this->y * this->y + this->z * this->z ; return sqrt( magsq ) ; } Look at these two bits of code: // To explicitly refer to yourself inside a method ThreeVector::magnitude( ) { float magsq = x * x + y * y + z * z ; return sqrt( magsq ) ; } These bits of code are Identical if you dont put the this-> in then the compiler does it for you !

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 Summary of Module : Pointers In this module we have covered the following topics. Pointers to primitive in-build types * to de-reference a pointer & to take the address of Pointers to objects The same as for in-built types In addition the -> operator for invoking methods

Technical Module : Pointers # /01Scientific Computing in OOCourse code 3C59 Use of Pointers to pass arguments A (not recommended) alternatice to references Historical as pointers existed before references Use of new and delete Dynamic allocation of memory Returns a pointer to the allocated memory You are responsible for keeping hold of the pointer you must delete the allocation when finished. Important use for destructors Destructors should delete any memory which an object allocates with new UNLESS responsibility for the pointer has been passed to some other object. this->