Advanced C++ 1
* declare pointers: pointerToRect is a pointer to Rectangle initially points to aRect dereference pointers: *pointerToRect = anotherRect all modify pointed-to object ( aRect ) 2 Rectangle *pointerToRect = &aRect; (*pointerToRect).size , pointerToRect − >size ,
& declare references: Rectangle &refToRect = aRect; , void print( const Rectangle &theRect); refToRect , theRect are references to Rectangle address-of: pointerToRect = &refToRect; &value is the address of value 3
recall: reference v pointer pointer — explicitly derference, can reassign reference — “bound” to object on creation, always refers to it typical implementation of both in asm: pointer 4
typed pointers double Z = 26.0; // ERROR “ cannot convert 'double*' to 'int*' in initialization ” C++ cares about type (but just addresses in assembly) 5 int *pointerToInt = &Z;
dereference example (1) int n = 26; cout << somePointer << endl; example output: (address will vary…) 0x7fff35fc3fe4 26 6 int *somePointer = &n; cout << *somePointer << endl;
dereference example (1) int n = 26; cout << somePointer << endl; example output: (address will vary…) 0x7fff35fc3fe4 26 6 int *somePointer = &n; cout << *somePointer << endl;
dereference example (2) int n = 26; *somePointer = 45; cout << somePointer << endl; example output: (address will vary…) 0x7fff35fc3fe4 45 7 int *somePointer = &n; cout << *somePointer << endl;
dereference example (2) int n = 26; *somePointer = 45; cout << somePointer << endl; example output: (address will vary…) 0x7fff35fc3fe4 45 7 int *somePointer = &n; cout << *somePointer << endl;
dereference example (3) ptr1 = new ListNode; ptr2 = new ListNode bool result1 = (ptr1 == ptr2); result1 defjnitely false (difgerent addresses) result2 probably true (depends on ListNode:: operator == ) 8 ListNode *ptr1, *ptr2; bool result2 = (*ptr1 == *ptr2);
dereference example (3) ptr1 = new ListNode; ptr2 = new ListNode bool result1 = (ptr1 == ptr2); result1 defjnitely false (difgerent addresses) result2 probably true (depends on ListNode:: operator == ) 8 ListNode *ptr1, *ptr2; bool result2 = (*ptr1 == *ptr2);
reference example int y = 5; int &x = y; cout << x << endl; cout << &x << endl; cout << &y << endl; x = 15; cout << y << endl; example output (address will vary…) 5 0x7ffeeda220d4 0x7ffeeda220d4 15 can’t change adderss stored in x 9
reference example int y = 5; int &x = y; cout << x << endl; cout << &x << endl; cout << &y << endl; x = 15; cout << y << endl; example output (address will vary…) 5 0x7ffeeda220d4 0x7ffeeda220d4 15 can’t change adderss stored in x 9
pointers to pointers int main() { Animal cow; ... } cow = Animal cowPtr1 = pointer to Animal cowPtr2 = pointer to (pointer to Animal) cowPtr3 = pointer to pointer to (pointer to Animal) 10 Animal* cowPtr1 = &cow; Animal** cowPtr2(&cowPtr1); Animal*** cowPtr3 = &cowPtr2;
pointers to pointers int main() { Animal cow; ... } cow = Animal cowPtr1 = pointer to Animal cowPtr2 = pointer to (pointer to Animal) cowPtr3 = pointer to pointer to (pointer to Animal) 10 Animal* cowPtr1 = &cow; Animal** cowPtr2(&cowPtr1); Animal*** cowPtr3 = &cowPtr2;
example memory layout address value … … … … memory cow cowPtr1 cowPtr2 cowPtr3 11 0x10000 0x500 0x10008 0x10000 0x10010 0x10008 0x10018 0x10010
ref to pointer v pointer to pointer } } if (*n == NULL) void insert(TreeNode*& n, int value) { 12 if (n == NULL) n = new TreeNode(value); else if (value < n − >value) insert(n − >left, value); else if (value > n − >value) insert(n − >right, value); void insert(TreeNode** n, int value) { *n = new TreeNode(value); else if (value < n − >value) insert(&(n − >left), value); else if (value > n − >value) insert(&(n − >right), value);
ref to pointer v pointer to pointer } } if (*n == NULL) void insert(TreeNode*& n, int value) { 12 if (n == NULL) n = new TreeNode(value); else if (value < n − >value) insert(n − >left, value); else if (value > n − >value) insert(n − >right, value); void insert(TreeNode** n, int value) { *n = new TreeNode(value); else if (value < n − >value) insert(&(n − >left), value); else if (value > n − >value) insert(&(n − >right), value);
ref to pointer v pointer to pointer } } if (*n == NULL) void insert(TreeNode*& n, int value) { 12 if (n == NULL) n = new TreeNode(value); else if (value < n − >value) insert(n − >left, value); else if (value > n − >value) insert(n − >right, value); void insert(TreeNode** n, int value) { *n = new TreeNode(value); else if (value < n − >value) insert(&(n − >left), value); else if (value > n − >value) insert(&(n − >right), value);
by ref versus by value void insert(TreeNode*& n, int value) { } } return n; return n; return new TreeNode(value); if (n == NULL) n = new TreeNode(value); } if (n == NULL) 13 else if (value < n − >value) insert(n − >left, value); else if (value > n − >value) insert(n − >right, value); TreeNode *insert(TreeNode* n, int value) { else if (value < n − >value) { n − >left = insert(n − >left, value); } else if (value > n − >value) { n − >right = insert(n − >right, value);
by ref versus by value void insert(TreeNode*& n, int value) { } } return n; return n; return new TreeNode(value); if (n == NULL) n = new TreeNode(value); } if (n == NULL) 13 else if (value < n − >value) insert(n − >left, value); else if (value > n − >value) insert(n − >right, value); TreeNode *insert(TreeNode* n, int value) { else if (value < n − >value) { n − >left = insert(n − >left, value); } else if (value > n − >value) { n − >right = insert(n − >right, value);
by ref versus by value void insert(TreeNode*& n, int value) { } } return n; return n; return new TreeNode(value); if (n == NULL) n = new TreeNode(value); } if (n == NULL) 13 else if (value < n − >value) insert(n − >left, value); else if (value > n − >value) insert(n − >right, value); TreeNode *insert(TreeNode* n, int value) { else if (value < n − >value) { n − >left = insert(n − >left, value); } else if (value > n − >value) { n − >right = insert(n − >right, value);
several memory allocation problems BROKEN: int someval(3); somePointer = &someVal; } int main() { someFunc(firstPointer); return 0; } pointer to deallocated memory — need new pass by value, not by reference 14 void someFunc( int *somePointer) { int *firstPointer; cout << *firstPointer << endl;
several memory allocation problems BROKEN: int someval(3); somePointer = &someVal; } int main() { someFunc(firstPointer); return 0; } pointer to deallocated memory — need new pass by value, not by reference 14 void someFunc( int *somePointer) { int *firstPointer; cout << *firstPointer << endl;
several memory allocation problems BROKEN: int someval(3); somePointer = &someVal; } int main() { someFunc(firstPointer); return 0; } pointer to deallocated memory — need new pass by value, not by reference 14 void someFunc( int *somePointer) { int *firstPointer; cout << *firstPointer << endl;
several memory allocation problems (fjxed?) somePointer = new int (3); } int main() { someFunc(firstPointer); return 0; } 15 void someFunc( int *&somePointer) { int *firstPointer; cout << *firstPointer << endl;
several memory allocation problems BROKEN: void someFunc() { } memory leak — never delete d 16 double *aliasPointer; aliasPointer = new double (6.27); cout << *aliasPointer << endl;
several memory allocation problems BROKEN: void someFunc() { } memory leak — never delete d 16 double *aliasPointer; aliasPointer = new double (6.27); cout << *aliasPointer << endl;
several memory allocation problems BROKEN: void someFunc() { double duration = 3.14; { { somePtr = &duration; } } return 0; } syntax error: somePtr no longer exists 17 double * somePtr; cout << *somePtr << endl;
several memory allocation problems BROKEN: void someFunc() { double duration = 3.14; { { somePtr = &duration; } } return 0; } syntax error: somePtr no longer exists 17 double * somePtr; cout << *somePtr << endl;
several memory allocation problems BROKEN: int main() { { int someVal(8); anotherPtr = &someVal; } return 0; } undefjned behavior: accessing uninitialized pointer 18 int * anotherPtr; cout << *anotherPtr << endl;
several memory allocation problems BROKEN: int main() { { int someVal(8); anotherPtr = &someVal; } return 0; } undefjned behavior: accessing uninitialized pointer 18 int * anotherPtr; cout << *anotherPtr << endl;
Recommend
More recommend