1 / 28

Pointer Review

Pointer Review. Node { int value; Node * next; Node( int val =-1, Node * n = NULL) {value = val ; next = n;} }. Given a ordered linked list pointed to by head, write the code to insert an element with value x.

bunny
Download Presentation

Pointer Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointer Review

  2. Node {int value; Node * next; Node( intval =-1, Node * n = NULL) {value = val; next = n;}} • Given a ordered linked list pointed to by head, write the code to insert an element with value x.

  3. Node {int value; Node * next; Node( intval =-1, Node * n = NULL) {value = val; next = n;}} • Given a ordered linked list pointed to by head, write the code to delete an element with value x.

  4. What type is a *a **a

  5. What type is a *a **a Hint: I read the declaration backwards, a is a pointer to a pointer to an int. When I look at *a, I mentally glue the *a together and think int * (*a) *a is a pointer to an int.

  6. Fill in the blanks //find and return the maximum value in an array intmaxEntry(int* data, intsize) { if ( data == NULL || size <= 0 ) return INT_MIN; int*highSoFar =________________ ; for (int count = ______; count <size;______________ ) { if ( __________________________) highSoFar= __________________; } return _____________________; }

  7. int * p;int * q; • What comparison would be true iffp and q have targets with the same value? 1) &p == &q 2) *p == *q 3) p == q

  8. Student * p;Student * q; • What comparison would be true iff p and q have the same target? 1) &p == &q 2) *p == *q 3) p == q

  9. intfoo= 17;int *ptr = &foo; • Which of the following statements will change the value of footo 18? 1) ptr++; 2) foo++; 3) (*foo)++; 4) (*ptr)++;

  10. Both code fragments below will compile but the one on the right will (on most systems) cause a runtime error. Why? Draw a picture to show what is happening. int x = 5; int *p = new int(x); delete p; int x = 5; int *p = &x; delete p;

  11. const intsize = 5;int*a = new int[size]; • What code fragment could be inserted in the blank in order to safely initialize each element of A to zero? int* p = &a[0]; for (inti= 0; i< size; i++, p++) { _____________________; } 1) *a = 0; 2) a[i] = 0; 3) *p = 0; 4) *i= 0;

  12. Definitions • 1) A dangling pointer is a pointer whose value is the address of memory that the program does not own. • 2) A memory leak is when the program owns memory that it can no longer access.

  13. const intsize = 5;int*a = new int[size]; • What logical error(s) would result if the following statement were executed? a= new int[2*size]; 1) A dangling pointer would result 2) A memory leak would result 3) Both a dangling pointer and a memory leak would result. 4) None of the above

  14. const intsize = 5;int*a = new int[size];int* p = &a[0]; • What logical error(s) would result if the following statement were executed: delete [] p; 1) A dangling pointer would result 2) A memory leak would result 3) Both a dangling pointer and a memory leak would result. 4) None of the above

  15. Show the picture after executing • p->next = q;

  16. Show the picture after executing q->next = p->next; p->next = NULL;

  17. Show the picture after executing q->next = p->next; q->next->next = p; p->next = NULL;

  18. Show the picture after executing • q->next = NULL; • delete p;

  19. Show the picture after executing • delete (p->next);

  20. Show the picture after executing • q->next = head; • delete p;

  21. Show the picture after executing • delete Head; • head = q;

  22. Given the following, explain the effect of the following on the contents of ptrand i: inti= 10,; int *ptr= &i intj = 4; *ptr+= j;

  23. Given the following declarationsint a[] = {5, 15, 34, 54, 14, 2, 52, 72};int *p = &a[1];int*q = &a[5]; • What is the value of *(p+3)?

  24. Given the following declarationsint a[] = {5, 15, 34, 54, 14, 2, 52, 72};int *p = &a[1];int*q = &a[5]; • What is the value of *(q-3)?

  25. Given the following declarationsint a[] = {5, 15, 34, 54, 14, 2, 52, 72};int *p = &a[1];int*q = &a[5]; • What is the value of q – p ?

  26. Given the following declarationsint a[] = {5, 15, 34, 54, 14, 2, 52, 72};int *p = &a[1];int*q = &a[5]; • Is the condition p < q true or false?

  27. Given the following declarationsint a[] = {5, 15, 34, 54, 14, 2, 52, 72};int *p = &a[1];int*q = &a[5]; • Is the condition *p < *q true or false?

More Related