CS360 Final -- December 10, 2002
Answers to Question 2
This is straightforward C code to do what the question asked. It is
in a2.c, and below:
/* Start time: Mon Dec 9 11:06:12 EST 2002
Stop time: Mon Dec 9 11:08:49 EST 2002 -- Timing -- I figure if I
can do this in under 3 minutes, you should be able to do it in
under 12 minutes. */
typedef struct flist {
int size;
int checksum;
struct flist *flink;
struct flist *blink;
} Flist;
void free(void *p)
{
int *x;
Flist *f;
x = (int *) p; /* First, set f to point 8 bytes before p */
x -= 2;
f = (Flist *) x;
if (f->checksum != 1234) { /* Check the checksum, and dump core if */
x = NULL; /* is not 1234 */
*x = 0;
}
f->checksum = 1235; /* Set it to 1235 */
f->blink = NULL; /* Put it in the front of the freelist */
f->flink = free_list;
if (f->flink != NULL) f->flink->blink = f;
free_list = f;
return;
}
Grading: 8 points
- Trying to set f to point 4 bytes before p: 1 point
- Correctly setting f to point 4 bytes before p: 1 point
- Checking the checksum, and doing something if it is not 1234: 1 point
- Actually dumping core correctly: 1 point
- Setting the checksum: 1 point
- Setting f->blink to NULL and f->flink to free_list: 1 point
- Setting free_list->blink to f (if it exists): 1 point
- Setting free_list to f: 1 point