ProgrammingModerate

A hash table stores pointers to nodes allocated with malloc. What must happen when the table is destroyed?

  1. A. Only the bucket array must be freed, since free on it releases nodes too
  2. B. Nothing, because the table owns no memory
  3. C. Each node is freed by calling free on the bucket index
  4. D. Every node must be freed exactly once, and the bucket array freed too

Correct answer: D. Every node must be freed exactly once, and the bucket array freed too

Each malloc'd node is owned by the table, so each must be freed once. The bucket array is a separate allocation and needs its own free.

More C Programming questions

Exams that ask this topic