CS360 Final -- December 11, 1999. Question 1: Answer and Grading

15 points


The answer

Your free() should do a few things: Sorry, I had a typo in the definition of FNode. The below definition is correct. I'm not sure if anyone noticed (it said struct freelist *flink instead of struct fnode *flink. Same with blink).

typedef struct fnode {
        int checksum;
        int size;
        struct fnode *flink;
        struct fnode *blink;
      } FNode;

extern FNode *Freelist;

#define DUMP_CORE { char *p = (char *) 0; *p = 0; }
void free(void *ptr)
{
  char *p;
  FNode *fn;

  p = (char *) ptr;

  /* Test to see if ptr is a multiple of 8: */
  if ( ((int)ptr) % 8 != 0) {
    fprintf(stderr, "free() not given a multilpe of 8\n");
    DUMP_CORE;
  }

  fn = (FNode *) ptr;
  fn -= 2;                /* Back up 8 bytes */

  /* Test the checksum */
  if ((int) fn != fn->checksum - 1) {
    fprintf(stderr, "free(): Bad checksum\n");
    DUMP_CORE;
  }

  /* Decrement the size and check it */
  fn->size--;
  if (fn->size < 16 || fn->size % 8 != 0) {
    fprintf(stderr, "free(): Bad size\n");
    DUMP_CORE;
  }

  /* Put fn onto the head of the freelist. */

  fn->blink = NULL;
  fn->flink = Freelist;
  if (fn->flink != NULL) fn->flink->blink = fn;
  Freelist = fn;

  return;
}

Grading:

I was surprised at how badly the majority of you did on this question. You did write free() as part of Lab 6, so I was surprised at the number of people who couldn't figure out the types of pointers or do the pointer arithmetic. Make sure you read the above answer and understand why it works. Also, when I say ``dump core'', that means make it dump core -- don't call exit(1).

Two more things -- I defined the linked list structure in the specification of the question. Many of you assumed that the linked list would be like dllists: circular with a sentinel node. That is not what I specified.

Also, you cannot call malloc(), sbrk() or dll_append() in free() -- you are supposed to be putting memory back on the freelist, not allocating more memory....

Here is the point allocation:

I was liberal about your omitting type casts and messing up types. Remember -- if you dereference a void * you get a void, which is not equivalent to an int or a char.