CS 2704 Homework 2: Review Spring 2001 Q A Reason For questions 1 through 8, you have to keep track of the difference between the pointer and the target of the pointer, as well as know a little bit about pointer arithmetic. 1 1 *p = 27 changes the value of the target of p (which is a), but not p. The initialization p = &a sets p's value to equal the address of a. 2 8 *p = 27 will assign the value 27 to a, since that's the target of p. 3 3 *p = 27 doesn't relocate p, so the address of p is still the same. 4 8 Incrementing a pointer increases it by an amount equal to the size of the type of its target in bytes. Since p points to an int, p++ will increase the value of p by 4, to 006AFDF8. 5 7 The target of p is the 4-byte int stored at the address 006AFDF8. We have NO information about the contents of that address. 6 2 b = int(&b) sets the value of b to the address of b. The typecast is necessary in order for the statement to compile. In any case, this won't change the address of b. 7 2 The value of b will be the address of b, which is still 006AFDF0. 8 2 9 7 1 obviously works. 2 is wrong because ptr++ will increment ptr, not its target. 3 is syntactically incorrect since Foo isn't a pointer. 4 works because (*ptr) IS the target of ptr. 10 3 p points to x, which is declared locally (not allocated dynamically). So, x is on the runtime stack, and delete p attempts to deallocate that memory. The process doesn't actually own the memory on the runtime stack, so this should blow up. 11 7 1 obviously works. 2 will assign the value 0 to A[0] over and over... 3 will work because p initially points to A[0] and each pass through the loop will increment p, making it point to the next cell of the array. 4 is syntactially incorrect since Idx isn't a pointer. 12 4 The loop assigns each array cell its address as a value. So, the value of A[3] is just the address of A[3] which is 007D0E7C. 13 2 This will allocate a new array, and assign its address to A. Since we didn't first delete the old array, allocated in the declaration above, that memory will still be owned by the program, but will no longer be accessible. 14 4 This will deallocate the array targeted by the pointer p. However, that is no longer the same array that is targeted by the pointer A, since the value of p was changed by the execution of the loop in question 11. There is no indication that the program even owns the memory to which p now points. So there's not a memory leak, nor is there a dangling pointer, but there is surely a logical error. 15 2 Obviously A must be an int*, given the description of the function and the first statement in the function body. In order for the function to actually DO anything, the value it assigns A (the address of the dynamically allocated array) must be available to the caller, so the pointer itself must be passed by reference. 16 5 1 resets the head pointer to point to the third Node, losing the first two. 2 is syntactically invalid since it assigns a pointer to a Node variable. 3 resets the first Node to point to the third Node, losing the second. 4 is syntactic nonsense. 17 2 headPtr->Next is a pointer to the second Node. Since the -> operator is associated from left to right, headPtr->Next->Volume is interpreted as (headPtr->Next)->Volume, which IS the Volume field of the second Node. 1 is attempts to assign an int to a pointer. 3 is treats headPtr->Next->Volume as if it were a pointer, when it's an int. 4 attempts to assign an int to a Node. 18 5 1-3 delete the first Node w/o saving the value of headPtr. 4 just sets the head pointer to point to the second Node w/o deleting the first one. The right way to do this is to save the value of headPtr->Next in a temp variable, then delete the first Node, then reset headPtr using the saved value. 19 1 The compiler detects only syntax errors. 20 8 If it compiles, the syntax is correct. If it doesn't produce correct output, the design logic must be wrong. 21 3 If it compiles, the syntax is correct. If it produces correct output on one test case, the design may be correct, or not. 22 6 Just trace this out. It's tedious, but not difficult as long as you have 23 6 a firm grasp of basic stream input. 24 1 The array parameter is delcared with "const", specifying that the function is NOT allowed to modify it. The assignment in the loop will result in a compile-time error. 25 2 In order for the function to work, the Point parameter P must be passed by reference, not by value. 26 3 The second parameter to the sort function should be the number of Point variables actually stored in the array, not the dimension of the array. This call will cause the sort function to compare real points with the dummy values stored in the tail of the array. Depending upon actual input data, some of those dummy Points may swap with real Points, which would be incorrect. This is all about the deep vs shallow copy issue. Hopefully you read up on this in the notes. 27 3 When myAlbum is passed by value, a shallow copy is made. That is, CD stores a pointer to the same array of Tracks as myAlbum. Now, CD is a local variable, so its destructor is invoked when the function terminates. The destructor will deallocate the array of Tracks. 28 1 When the calling function terminates, the destructor for myAlbum will be invoked. This will attempt to deallocate memory that the program no longer owns; typically causing a runtime error. 29 1 The copying of a parameter which is passed by value is carried out by the class copy constructor. The class implementation needs to be extended to include a copy constructor which takes care of making a duplicate of the array in the source object. 30 2 The copy constructor is not enough for a robust class implementation. If we performed an assignment, the same shallow copy problem would arise unless we also implemented an overloaded version of the assignment operator to perform a deep copy.