LearnFrenzy
Sign in Take a free test

Memory Allocation · Memory Allocation MCQ · Question 6

Stack vs heap: where is malloc() memory? Answer, explanation & discussion

  • MCQ
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links