ProgrammingModerate
A program makes three malloc calls and two free calls, with no other allocations. How many blocks are still allocated when the program ends, if it leaks nothing else?
- A. 0
- B. 2
- C. 1
- D. 3
Correct answer: C. 1
Each malloc creates one block and each free releases one. Three minus two leaves one block that was never freed.
More C Programming questions
- int a = calloc(4, sizeof a); int sum = 0; for (int i = 0; i < 4; i++) sum += a[i]; printf("%d", sum); What is printed?
- Which statement about calling free on a null pointer is correct in C?
- A pointer p is freed with free(p) and then set to NULL. Why is this safer than leaving it dangling?
- Which code creates a flexible structure holding n ints after a header, with a single allocation and correct size?
- A tail-insertion function takes a pointer to the head pointer, so it can work on an empty list. Which loop finds the link to change?
- A queue with head and tail pointers is emptied by dequeuing its only node. What must the dequeue do to keep the queue correct?