280 likes | 384 Views
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.
E N D
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.
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.
What type is a *a **a
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.
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 _____________________; }
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
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
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)++;
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;
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;
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.
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
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
Show the picture after executing • p->next = q;
Show the picture after executing q->next = p->next; p->next = NULL;
Show the picture after executing q->next = p->next; q->next->next = p; p->next = NULL;
Show the picture after executing • q->next = NULL; • delete p;
Show the picture after executing • delete (p->next);
Show the picture after executing • q->next = head; • delete p;
Show the picture after executing • delete Head; • head = q;
Given the following, explain the effect of the following on the contents of ptrand i: inti= 10,; int *ptr= &i intj = 4; *ptr+= j;
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)?
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)?
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 ?
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?
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?