ProgrammingModerate

A list of N nodes is freed with free() on each node but the next pointer is read after freeing it. Which order is correct?

  1. A. Save next in a temporary before calling free on the current node
  2. B. Read next after free, since the block is still valid
  3. C. Free the nodes in random order
  4. D. Set every next to NULL before freeing, which frees nothing

Correct answer: A. Save next in a temporary before calling free on the current node

free invalidates the node's memory, so the next pointer must be copied out before the free call.

More C Programming questions

Exams that ask this topic