ProgrammingModerate

A function frees an entire list and then sets head = NULL. Why is setting head to NULL useful?

  1. A. It causes free to return memory to the operating system immediately
  2. B. It makes the next allocation return the same block
  3. C. It is required by the C standard for every free
  4. D. It stops later code from reading or freeing the old, now dangling, pointer

Correct answer: D. It stops later code from reading or freeing the old, now dangling, pointer

A NULL pointer is safe to test, and a second free of a NULL pointer is a no-op. A dangling pointer is a hazard.

More C Programming questions

Exams that ask this topic