ProgrammingVery hard

Three rows are each malloc'd separately, and their pointers are stored in an array rowp[3]. Which cleanup order is correct?

  1. A. Free rowp first, then the rows
  2. B. Free only rowp, since the rows are inside it
  3. C. Free rowp[0] once, which releases all three rows
  4. D. Free each row first, then free rowp

Correct answer: D. Free each row first, then free rowp

The row blocks are separate allocations, so each needs its own free. The pointer array is freed last, after no row pointer is still needed.

More C Programming questions

Exams that ask this topic