Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Pointer Review

2

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

4 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 delete an element with value x.

5 What type is a *a **a

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

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

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

9 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

10 int foo = 17; int *ptr = &foo; Which of the following statements will change the value of foo to 18? 1) ptr++; 2) foo++; 3) (*foo)++; 4) (*ptr)++;

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

12 const int size = 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 (int i = 0; i< size; i++, p++) { _____________________; } 1) *a = 0; 2) a[i] = 0; 3) *p = 0; 4) *i= 0;

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

14 const int size = 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

15 const int size = 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

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

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

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

19 Show the picture after executing q->next = NULL; delete p;

20 Show the picture after executing delete (p->next);

21 Show the picture after executing q->next = head; delete p;

22 Show the picture after executing delete Head; head = q;

23 Given the following, explain the effect of the following on the contents of ptr and i: int i = 10,; int *ptr = &i int j = 4; *ptr += j;

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

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

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

27 Given the following declarations int 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?

28 Given the following declarations int 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?


Download ppt "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."

Similar presentations


Ads by Google