ProgrammingModerate

A program calls free(p) and then reads p->v in the next line. What is the correct description?

  1. A. It prints the old value, because free only marks the memory as unused
  2. B. This is undefined behaviour, because p refers to memory that has been freed
  3. C. It is defined if p is not NULL
  4. D. It is an error at compile time

Correct answer: B. This is undefined behaviour, because p refers to memory that has been freed

Reading an object after free is undefined, even if the old value often still appears. Compilers do not necessarily report it.

More C Programming questions

Exams that ask this topic