ProgrammingHard

A program creates an array of 10^6 pointers and then one malloc block per pointer, with 10^6 frees at the end. What is the total number of malloc calls and free calls?

  1. A. 10^6 mallocs and 10^6 frees in total
  2. B. 10^6 mallocs and no frees, since the array frees the blocks
  3. C. 10^6 mallocs and 10^6 frees, plus one free for the pointer array
  4. D. 2 x 10^6 mallocs and 10^6 frees

Correct answer: C. 10^6 mallocs and 10^6 frees, plus one free for the pointer array

The pointer array is one allocation that needs its own free, so the count of frees is one more than the number of blocks.

More C Programming questions

Exams that ask this topic