Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 C++ Pointers Gordon College. 2 Regular variables Regular variables declared –Memory allocated for value of specified type –Variable name associated."— Presentation transcript:

1 1 C++ Pointers Gordon College

2 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 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 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 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 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 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 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 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 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 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 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 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 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 108 7 p_cow int* 110108 p_p_cow int** 112 110 p_p_p_cow int*** 114 112 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 15 Pointer to objects Declaration: classType *name = &object Accessing the object: (*name).method(); name->method();

16 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.1.2001", "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; }


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

Similar presentations


Ads by Google