Instructions: This is a closed book, closed note exam. Calculators are not permitted. If you have a question, raise your hand and I will come to you. Please work the exam in pencil and do not separate the pages of the exam. For maximum credit, show your work. Good Luck! Your Name (please print) 1 2 3 4 25 20 30 25 100 1
Problem 1 (25 points) Hash Tables Consider an open hash table composed of a four-bucket table, with each bucket containing a variable length list. Each list entry has three slots <key, value, next> corresponding to the three word groupings in the entries section. The hash function is key mod four. Inserted entries are appended to the end of a bucket list. Deallocated entries are maintained on a LIFO free list. When the free list is empty, new entry objects are allocated from heap memory. Accesses are listed as <op, key, [value]>. Simulate the access list below and draw the ending state. Assume the heap pointer is initially 5040 and the free pointer is initially 0. Heap 5040 Free 0000 Pointer List Buckets 5000 5004 5016 5008 5028 5012 Entries 5016 37 5040 5064 5088 5020 555 5044 5068 5092 5024 0 5048 5072 5096 5028 18 5052 5076 5100 5032 333 5056 5080 5104 5036 0 5060 5084 5108 Hash Table Access Trace # op key value # op key value 1 insert 25 111 5 remove 18 n/a 2 insert 12 222 6 remove 25 n/a 3 insert 37 444 7 insert 40 888 4 insert 41 777 8 insert 15 999 2
Problem 2 (3 parts, 20 points) Associative Set Performance and Short Answer Part A (9 points) Consider a hash table that uses 11 buckets, each containing a list of items. Suppose the hash table contains 220 entries total and the entries are evenly distributed across the hash table buckets. Assume that computing the hash function takes an average of three operations and comparing two keys takes an average of eight operations. Ignore effects of spatial and temporal reference locality. Suppose that 75% of keys looked up are found in the hash table and 25% are not found. How many of these operations would be required for the average lookup in the hash table described above if the bucket list is unsorted versus sorted? (Show work.) number of operations when each bucket list is unsorted: number of operations when each bucket list is sorted: Part B (7 points) Suppose the hash table automatically resizes (with the same 220 entries and found key probabilities) so that the average access time becomes 26 operations. How many hash table buckets are being used, assuming each bucket contains an unsorted list of entries? (Show work.) New number of buckets: Part C (4 points) Describe one advantage that old-space/new-space copying garbage collection has over a mark-and-sweep garbage collection strategy. 3
Problem 3 (6 parts, 30 points) Heap Management Below is a snapshot of heap storage. Values that are pointers are denoted with a $. The heap pointer is $6172. The heap has been allocated contiguously beginning at $6000, with no gaps between objects. addr value addr value addr value addr value addr value addr value 6000 16 6032 12 6064 36 6096 16 6128 12 6160 0 6004 32 6036 $6100 6068 4 6100 $6052 6132 4 6164 4 6008 4 6040 10 6072 20 6104 16 6136 6032 6168 0 6012 16 6044 4 6076 8 6108 $6080 6140 0 6172 0 6016 8 6048 16 6080 8 6112 152 6144 24 6176 0 6020 8 6052 $6024 6084 6004 6116 8 6148 0 6180 0 6024 0 6056 $6100 6088 4 6120 16 6152 16 6184 0 6028 $6052 6060 8 6092 $6080 6124 6156 6156 12 6188 0 Part A (4 points) Assuming the heap has been allocated contiguously beginning at 6000, with no gaps between objects, circle all object size words in the map above. Part B (6 points) Suppose the stack holds a local variable whose value is the memory address $6036 and register $5 holds the address $6092. No other registers or static variables currently hold heap memory addresses. List the addresses of all objects in the heap that are not garbage. Addresses of Non-Garbage Objects: Part C (8 points) Create a free list by scanning the heap memory for garbage, starting at address 6000 and placing objects on the free list so that a first-fit allocation strategy will also always produce the best-fit. Free List: Part D (3 points) Based on the free list created in part C, if an object of size 26 bytes is allocated, what address will be returned using a first-fit allocation strategy? How many bytes of slack (if any) will result? Address: Slack: Part E (3 points) Based on the free list created in part C, if an object of size 5 bytes is allocated, what address will be returned using a best-fit allocation strategy? How many bytes of slack (if any) will result? Address: Slack: Part F (6 points) If the local variable whose value is the address $6036 is popped from the stack, which addresses will be reclaimed by each of the following strategies? If none, write none. (You need not list addresses already on the free list from part C.) Reference Counting: Mark and Sweep: Old-New Space (copying): 4
Problem 4 (2 parts, 25 points) Linked Lists Consider the following declarations. typedef struct Car { int Year; int Tag; struct Car *Next; Car; Car *KnownCars = Null; Part A (12 points) Write a C program Register_Vehicle that takes two integers (ModelYear and TagNum) as arguments. It should do the following: 1. allocate space on the heap for a new Car object, 2. print an error message if there is not enough heap space, 3. initialize the fields Year and Tag to the given inputs ModelYear and TagNum, and 4. push the new Car object on KnownCars. void Register_Vehicle(int ModelYear, int TagNum) { Part B (13 points) Complete the following C subroutine to remove a Car object from KnownCars if it has the input TagNum as its Tag field. Assume that there is at most one Car object in KnownCars with that TagNum as its Tag (so there may be none with that matching Tag). Be sure that your code does not create a memory leak. void Remove_Vehicle(int TagNum) { Car *ThisCar = KnownCars; Car **TrailingPointer = &KnownCars; /* Move ThisCar pointer and TrailingPointer down the KnownCars list while the end of the list is not reached and Car with TagNum is not found */ while( ){ /* If end of list is not reached (so Car with TagNum is found), splice that Car out of the list of KnownCars. Prevent memory leak. */ if ( ) { 5