DOC PREVIEW
UD CISC 181 - Lecture 18

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 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 13 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 13 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 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CISC181 Introduction to Computer ScienceDr McCoy1Dr. 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.2Basic 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 objectConstructor 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 lik e other types• Can be function parameters•Can be returned from functions•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 messageProduces error message• Still good practice to use NULL check10‐12Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.3Freestore 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: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 )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 variable!• Example:int a[10];int * p;– a and p are both pointer variables!10‐17Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Array Variables  Pointers• Recall previous example:int a[10];typedef int* IntPtr;IntPtr p;•a and p are pointer variablesa and p are pointer variables– Can perform assignments:p = a; // Legal.• p now points where a points– To first indexed variable of array a– a = p; // ILLEGAL!• Array pointer is CONSTANT pointer!10‐18Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.4Array Variables  Pointers• Array variableint a[10];• MORE than a pointer variable– "const int *" type– Array was allocated in memory already– Variable a MUST point there…always!• Cannot be changed!• In contrast to ordinary pointers– Which can (& typically do) change10‐19Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Dynamic Arrays• Array limitations– Must specify size first– May not know until program runs!•Must "estimate" maximum size needed•Must "estimate" maximum size needed– Sometimes OK, sometimes not– "Wastes" memory• Dynamic arrays– Can grow and shrink as needed10‐20Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Creating Dynamic Arrays• Very simple!• Use new operator– Dynamically allocate with pointer variable– Treat like standard arrays• Example:typedef double * DoublePtr;DoublePtr d;d = new double[10]; //Size in brackets– Creates dynamically allocated array variable d,with ten elements, base type double10‐21Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Deleting Dynamic Arrays• Allocated dynamically at run‐time– So should be destroyed at run‐time• Simple again. Recall Example:d = new double[10];//P i…


View Full Document
Download Lecture 18
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 Lecture 18 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 Lecture 18 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?