260 likes | 908 Views
C++ Pointers. Gordon College. 27. 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). Pointers. Pointer Variables contains a memory address
E N D
C++ Pointers Gordon College
27 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)
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 &
Basic Pointer Operations • Dereferencing and indirection • Pointer variable stores address of a location • Accessing contents of that location requires dereferencing operator * cout << *iPtr; Dereferencing pointer int *iPtr = &i; Pointer declaration PointerFun Video
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;
Basic Pointer Operations • Consider: *jPtr = 44; • Changes value that both pointers reference • Not good programming practice, hard to debug • Known as aliasing problem
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)
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 <cstdlib> . . . Node* head = NULL; // Initialized pointer to NULL. WARNING: Invalid addresses in pointer can cause problems - known as “wild pointers” when not initialized
Pointers vs. References • Consider this code: int i; 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?
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; } signedmain() { char the_alphabet[] = "abcdefghijklmnopqrstuvwxyz"; char* p_t = GetFirstT(the_alphabet); cout << p_t - the_alphabet << endl; } OUTPUT: 19
Dynamic Memory Allocation • The new operation • Exampleint * intPtr; intPtr = new int; • An anonymous variable • Cannot be accessed directly • Warning: watch out for memory leak
Memory Management Problems • Memory leak - program fails to release memory when no longer needed { int *t = newint(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
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
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 Variable Type of Variable Address in Memory Value Stored Cow Int 108 7 p_cow int* 110 108 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;
Pointer to objects • Declaration: classType *name = &object • Accessing the object: (*name).method(); name->method();
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 << "Bill for: " << kennel[i] -> getName() << endl << endl; kennel[i] -> printBill(); cout << endl << dashes << dashes << endl; }