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?
- A. 10^6 mallocs and 10^6 frees in total
- B. 10^6 mallocs and no frees, since the array frees the blocks
- C. 10^6 mallocs and 10^6 frees, plus one free for the pointer array
- 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
- What is the effect of realloc(p, 0) when p came from malloc, in the C standard's terms?
- A function returns a pointer to a dynamically allocated copy of its argument string. Which signature is the most correct for the caller's…
- int m = malloc(3 sizeof m); for (int i = 0; i < 3; i++) m[i] = malloc(4 sizeof m); What is the number of allocation calls made in total?
- A doubly linked list node has prev and next. What must happen before freeing the node p in the middle of the list?
- A linked stack stores nodes with a pointer to the top. Push and pop run in O(1). What is the cost of freeing the whole stack when it has n…
- A list of 10^6 nodes is built by prepending, and then each node is looked up by value in a loop. What is the total cost of 10^6 lookups?