LearnFrenzy
Sign in Take a free test

Memory Allocation · Predict the Output · Question 1

sizeof p vs sizeof *p after malloc – output Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = malloc(10 * sizeof *p);
    if (p == NULL)
        return 1;
    printf("%zu %zu\n", sizeof p, sizeof *p);
    free(p);
    return 0;
}
  • A 8 4 Correct
  • B 40 4
  • C 40 40
  • D 8 40
Correct Answer: Option A

sizeof p is the size of the pointer variable (8 bytes); sizeof *p is one int (4). Nothing in C remembers that the block holds 40 bytes – you must keep the count yourself.

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.

Popular