CS 1704 Fall 2003 Homework 3 Answers Q A Reason -------------------------------------------------------------------------------- 1 2 Line 1 declares a pointer and assigns it the value NULL. But, there's been no dynamic allocation of a target, and the address 0 is not the address of a valid target. 2 3 Line 2 allocates a target for the pointer, but the target has not yet been assigned a value. 3 4 Line 3 assigns a value to the target of the pointer. For questions 4 through 6, the key to what happens is how a function call is handled. As we've said, each function call leads to the creation of a record on the runtime stack, and that record is logically removed when the function terminates. 4 2 This probably wasn't obvious unless you actually tested the code. F() returns a pointer to a local variable, which has the value 42. But, when G() is called, its record overwrites the one for F(). Since G() the body of G() is structured the same way as F(), the local variable j in G() is in the same spot in memory as the local variable i in F(). So, when the address returned by F() is accessed, we find the value of the variable j in G()... 5 3 6 4 The call to G() doesn't change the value of the pointer f, it just overwrites the memory space where the target of f was stored. 7 2 To compare the values of the targets, you must dereference the pointers. 8 1 The targets are the same (not just have the same value) if they are at the same address; the addresses are stored in the pointers. 9 1 This is logically equivalent to question 8. 10 2 We need to eliminate the label that precedes the integer value. 1 is syntactically invalid. 3 would ignore the entire remainder of the file. 11 1 Aside from testing the code... the parameter declaration specifies the name of an array, which is a pointer to the 0th cell. 12 7 The declaration of the parameter is best understood by reading it from right to left. It says that Data is a constant pointer to an int which is also constant. The first occurrence of const applies to the target of the pointer; the second to the pointer. 13 2 No. Line 4 declares that the TARGET of the pointer cannot be changed; Line 7 will modify the pointer itself, not its target. 14 3 This must add the value of the TARGET of the pointer to Sum; so the pointer must be dereferenced using the * operator. 15 1 This must be where the pointer is stepped to the next array cell. If that isn't done, the loop will not access the elements properly. 16 6 The address of the first array cell is stored in the array pointer Data; the address can also be obtained by applying the address operator (&) to the name of the cell (Data[0]). 1 would give the address of the array pointer, which is wrong. 2 would give the value stored in Data[0], which is wrong. 4 would also give the value stored in Data[0]. 17 5 Using the same logic as in question 7, &Data[1] gives the address of the second array cell. 1 is wrong because hiSoFar stores the address of the first cell. 2 would give the address of the second cell, but it would also change the value of hiSoFar, which would mess up the logic. 3 wouldn't compile since Data is a const pointer. 4 would give the value stored in the second array cell. 18 3 Count must be incremented or the loop will be infinite. Current must be incremented in order to step through the array; since that isn't accounted for in the loop body, it must be in the update section of the loop header. 19 1,4 This must compare the largest value seen so far to the current value; the pointers must be dereferenced in order to get the data values. 2 would compare the pointer values, not the values of their targets. 3 would compare the ADDRESSES of the pointers. 4 is fine since it's the same as 1. 20 2 The function is supposed to return the maximum value found in the array. That would be the target of hiSoFar. 1 would return the address of the maximum value. 3 would return the address of the pointer. 4 would be syntactically illegal since the function must return an int. 21 1 From the table, the address of A is 0012FED4. 22 2,4 From the table, the value of A is 002F1090. 23 3,5 From the table, the value stored at the address 002F1090 is 0000002A. 24 3 The comparison makes sure that the array cell contains a pointer with a target; since that pointer is about to be reset, its target needs to be deallocated or it will be inaccessible. 25 1 The loop counts the number of values read; but if the loop terminates early because the input stream fails, the counter won't equal the expected number of input values. 26 1 The first part of the test makes sure that an array has been allocated; the second part makes sure the array is supposed to hold at least one value. But even if the array didn't hold any values, line 5 wouldn't cause an access violation. 27 2 Line 6 would cause an access violation if the pointer stored in A[Pos] was NULL, so this check prevents that error. (Of course, there could still be an access violation if the pointer value is invalid, but there's no way to check that.) 28 2 The first test must make sure that A[Pos] isn't NULL, since that pointer is about to be dereferenced to make the search comparison. 29 1 The second test must check to see if the target of the current pointer is equal to the search string. 30 4 If the order were reversed, then we'd try to dereference the pointer before we check to be sure it isn't NULL.