ProgrammingModerate

A function returns a pointer to a node created with malloc inside it. Which description is correct?

  1. A. The caller must not free it because malloc memory frees itself at return
  2. B. The caller must free it with delete
  3. C. The pointer becomes invalid as soon as the function returns, so it cannot be used
  4. D. The caller must eventually call free on the returned pointer exactly once

Correct answer: D. The caller must eventually call free on the returned pointer exactly once

Heap memory outlives the function that allocated it. Ownership passes to the caller, who must free it once, and it is not released when the function returns.

More C Programming questions

Exams that ask this topic