DOC PREVIEW
UD CISC 181 - Pointer Assignments

This preview shows page 1-2-3-4-5-36-37-38-39-40-72-73-74-75-76 out of 76 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 76 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CISC181 Introduction to Computer Science Dr. McCoy Lecture 18 October 29, 2009Pointer AssignmentsPointer Assignments Graphic: Display 10.1 Uses of the Assignment Operator with Pointer VariablesThe new OperatorBasic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (1 of 2)Basic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (2 of 2)Basic Pointer Manipulations Graphic: Display 10.3 Explanation of Display 10.2More on new OperatorPointers and FunctionsMemory ManagementChecking new Successnew Success – New CompilerFreestore Sizedelete OperatorDangling PointersDynamic ArraysArray VariablesArray Variables  PointersSlide 19Slide 20Creating Dynamic ArraysDeleting Dynamic ArraysFunction that Returns an ArrayDestructor NeedDestructorsCopy ConstructorsSummary 1Summary 2Go over Homework6.5 Implementing a Time Abstract Data Type with a class6.6 Class Scope and Accessing Class MembersSlide 326.6 Class Scope and Accessing Class Membersfig06_04.cpp (1 of 2)fig06_04.cpp (2 of 2) fig06_04.cpp output (1 of 1)Another Example6.7 Separating Interface from Implementation6.7 Separating Interface from Implementationtime1.h (1 of 1)time1.cpp (1 of 3)time1.cpp (2 of 3)time1.cpp (3 of 3)fig06_07.cpp (1 of 2)fig06_07.cpp (2 of 2) fig06_07.cpp output (1 of 1)6.8 Controlling Access to Membersfig06_08.cpp (1 of 1)fig06_08.cpp output (1 of 1)6.8 Controlling Access to Members6.9 Access Functions and Utility Functionssalesp.h (1 of 1)salesp.cpp (1 of 3)salesp.cpp (2 of 3)salesp.cpp (3 of 3)fig06_11.cpp (1 of 1)fig06_11.cpp output (1 of 1)6.10 Initializing Class Objects: Constructors6.11 Using Default Arguments with Constructorstime2.h (1 of 1)time2.cpp (1 of 3)time2.cpp (2 of 3)time2.cpp (3 of 3)fig06_14.cpp (1 of 2)fig06_14.cpp (2 of 2)fig06_14.cpp output (1 of 1)6.12 Destructors6.14 Using Set and Get Functionstime3.h (1 of 2)time3.h (2 of 2)time3.cpp (1 of 4)time3.cpp (2 of 4)time3.cpp (3 of 4)time3.cpp (4 of 4)fig06_20.cpp (1 of 3)fig06_20.cpp (2 of 3)fig06_20.cpp (3 of 3)fig06_20.cpp output (1 of 1)1CISC181 Introduction to Computer ScienceDr. McCoyLecture 18October 29, 2009Pointer Assignments•Pointer variables can be "assigned":int *p1, *p2;p2 = p1;–Assigns one pointer to another–"Make p2 point to where p1 points"•Do not confuse with:*p1 = *p2;–Assigns "value pointed to" by p1, to "valuepointed to" by p210-2Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Pointer Assignments Graphic: Display 10.1 Uses of the Assignment Operator with Pointer Variables10-3Copyright © 2010 Pearson Addison-Wesley. All rights reserved.The new Operator•Since pointers can refer to variables…–No "real" need to have a standard identifier•Can dynamically allocate variables–Operator new creates variables•No identifiers to refer to them•Just a pointer!•p1 = new int;–Creates new "nameless" variable, andassigns p1 to "point to" it–Can access with *p1•Use just like ordinary variable10-4Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Basic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (1 of 2)10-5Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Basic Pointer Manipulations Example: Display 10.2 Basic Pointer Manipulations (2 of 2)10-6Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Basic Pointer Manipulations Graphic: Display 10.3 Explanation of Display 10.210-7Copyright © 2010 Pearson Addison-Wesley. All rights reserved.More on new Operator•Creates new dynamic variable•Returns pointer to the new variable•If type is class type:–Constructor is called for new object–Can invoke different constructor withinitializer arguments:MyClass *mcPtr;mcPtr = new MyClass(32.0, 17);•Can still initialize non-class types:int *n;n = new int(17); //Initializes *n to 1710-8Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Pointers and Functions•Pointers are full-fledged types–Can be used just like other types•Can be function parameters•Can be returned from functions•Example:int* findOtherPointer(int* p);–This function declaration:•Has "pointer to an int" parameter•Returns "pointer to an int" variable10-9Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Memory Management•Heap–Also called "freestore"–Reserved for dynamically-allocated variables–All new dynamic variables consume memoryin freestore•If too many  could use all freestore memory•Future "new" operations will fail if freestoreis "full"10-10Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Checking new Success•Older compilers:–Test if null returned by call to new:int *p;p = new int;if (p == NULL){ cout << "Error: Insufficient memory.\n"; exit(1);}–If new succeeded, program continues10-11Copyright © 2010 Pearson Addison-Wesley. All rights reserved.new Success – New Compiler•Newer compilers:–If new operation fails:•Program terminates automatically•Produces error message•Still good practice to use NULL check10-12Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Freestore Size•Varies with implementations•Typically large–Most programs won’t use all memory•Memory management–Still good practice–Solid software engineering principle–Memory IS finite•Regardless of how much there is!10-13Copyright © 2010 Pearson Addison-Wesley. All rights reserved.delete Operator•De-allocate dynamic memory–When no longer needed–Returns memory to freestore–Example:int *p;p = new int(5);… //Some processing…delete p;–De-allocates dynamic memory "pointed to bypointer p"•Literally "destroys" memory10-14Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Dangling Pointers•delete p;–Destroys dynamic memory–But p still points there!•Called "dangling pointer"–If p is then dereferenced ( *p )•Unpredicatable results!•Often disastrous!•Avoid dangling pointers–Assign pointer to NULL after delete:delete p;p = NULL;10-15Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Dynamic Arrays•Array variables–Really pointer variables!•Standard array–Fixed size•Dynamic array–Size not specified at programming time–Determined while program running10-16Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Array Variables•Recall: arrays stored in memoryaddresses, sequentially–Array variable "refers to" first indexed variable–So array variable is a kind of pointer


View Full Document
Download Pointer Assignments
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Pointer Assignments and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Pointer Assignments 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?