Machine-translated by Google. Questions are authored in English.
Top

Discussion

How many bytes does the structure below occupy?

#include <stdio.h>
struct test
{
  int k;
  char c;
};

 

  • A.Multiple of integer size
  • B.Integer size+character size
  • C.Depends on the platform
  • D.Multiple of word size

Answer: A

Correct answer: Multiple of integer size

The members are an int and a char, but the compiler adds padding so that the whole structure aligns to its strictest member. The size therefore comes out a multiple of the int size - 8 bytes on a typical machine, not the 5 the members occupy. Padding is what makes sizeof a structure unpredictable from its members alone.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.