Question 6
Back to Exercise
In void f(void) { int x; int *p = malloc(sizeof *p); }, where are x, p and the allocated int?
- A All on the heap
- B x on the stack; p and the int on the heap
- C All on the stack
- D x and p on the stack; the allocated int on the heap Correct
Correct Answer: Option D
x and the pointer variable p are locals (stack) and vanish when f returns. The block p points to is on the heap and survives – here it leaks, because nothing frees it before p disappears.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!