CSC161 2010F, Class 56: Discussion of Exam 3 Overview: * The final. * Some thoughts on exam 3. * Final comments. Admin: * It is neither national pipe cleaner day nor national chenille stem day. Nonetheless, we are celebrating. * Our final exam is next Wednesday at 9 a.m. in this room. * Three hours * You should have received tentative grades. Let me know if you did not. * It's computer science education week, and Mattel has released Computer Engineer Barbie! * Do you want Aditi to run a review session? The Final * Four questions * You will have limited computer access * You will answer questions on the computer, using code I provide * Steps: Copy tarball, untar tarball, work, tar, email back to Sam * You will be able to use any shell commands, including man * NO WEB ACCESS * NO ACCESS TO OUR EXAMPLES DIRECTORY (Please do not make a massive copy of it before the exam.) * You can access your old homework * You can access any EBoards (if you know where they are) I'll tell you where they are. * One "fix this code" question * At least one problem quite similar to a previous exam problem * I will provide unit tests where appropriate Exam 3 * General issues * Way too many memory leaks. char *str = malloc (128); str = strdup(target); * Carelessness in the amount of memory you allocate. Goal: Make str a copy of target. char *str = malloc (strlen (target) * sizeof (char)); strcpy (str, target); * This allocated one character too few char *str = malloc (sizeof (target)) strcpy (str, target); * sizeof does not give the length of a string; sizeof indicates, for a variable or type, how much memory that variable or type requires. target holds a pointer to a char, so it requires FOUR BYTES * Forgets to allocate memory char *stuff[12]; // An array of 12 strings for (i = 0; i < 12; i++) strcpy (stuff[i], whatever); Problem 1 * See sample code Problem 4 * Inserting into lists * Lots of inefficiencies node1 = find_before (lst, target); node2 = find (lst, target); * node2 = node1->next would be much more efficient * Some code that makes me unhappy node1 = lst->head; while (strcmp (node1->data, target) != 0) node1 = node1->next; * Yes, it violates the preconditions, but segfaults are not a good way to indicate that. Problem 6 struct string_list * slist_transform (struct string_list *list, void (*fun)(char *str)) { struct node *n = list->head; while (n != NULL); { (*fun)(n->data); n = n->next; } return list; } Problem 7