advanced c
play

Advanced C++ 1 * declare pointers: pointerToRect is a pointer to - PowerPoint PPT Presentation

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;


  1. Advanced C++ 1

  2. * 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 ,

  3. & 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

  4. 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

  5. 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;

  6. dereference example (1) int n = 26; cout << somePointer << endl; example output: (address will vary…) 0x7fff35fc3fe4 26 6 int *somePointer = &n; cout << *somePointer << endl;

  7. dereference example (1) int n = 26; cout << somePointer << endl; example output: (address will vary…) 0x7fff35fc3fe4 26 6 int *somePointer = &n; cout << *somePointer << endl;

  8. 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;

  9. 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;

  10. 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);

  11. 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);

  12. 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

  13. 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

  14. 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;

  15. 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;

  16. example memory layout address value … … … … memory cow cowPtr1 cowPtr2 cowPtr3 11 0x10000 0x500 0x10008 0x10000 0x10010 0x10008 0x10018 0x10010

  17. 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);

  18. 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);

  19. 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);

  20. 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);

  21. 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);

  22. 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);

  23. 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;

  24. 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;

  25. 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;

  26. 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;

  27. several memory allocation problems BROKEN: void someFunc() { } memory leak — never delete d 16 double *aliasPointer; aliasPointer = new double (6.27); cout << *aliasPointer << endl;

  28. several memory allocation problems BROKEN: void someFunc() { } memory leak — never delete d 16 double *aliasPointer; aliasPointer = new double (6.27); cout << *aliasPointer << endl;

  29. 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;

  30. 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;

  31. 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;

  32. 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