University of Virginia, Department of Computer Science
CS201J: Engineering Software, Fall 2002

Notes: Thursday Halloween 2002
Schedule

Null Pointer Dereferences

What can happen if a C program dereferences a null pointer?




How can we detect and avoid null pointer dereferences?




Memory Management in C

What happens if a program does not release the memory it allocates?




How can programmers document assumptions about memory management?





Malloc and Free

/*@only@*/ /*@null@*/ void * malloc(size_t bytes);
void free(/*@only@*/ void * ptr);
Species Constructor
/*@only@*/ Species Species_new (/*@only@*/ char *name,
                                /*@only@*/ char *genome) {
  Species s = (Species) malloc (sizeof (*s));
  if (s == NULL) {
    fprintf (stderr, "Out of memory in Species_new.\n");
    exit(EXIT_FAILURE);
  }

  s->name = name;
  s->genome = genome;
  return s;
}

CS201J University of Virginia
Department of Computer Science
CS 201J: Engineering Software
Sponsored by the
National Science Foundation
cs201j-staff@cs.virginia.edu