ProgrammingVery hardProduct company OA

Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed?

Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed?
struct A { char c; double d; int i; };
struct B { double d; int i; char c; };
struct C { char c; short s; char d; int i; };
struct D { struct C x; char t; double *p; };
printf("%zu %zu %zu %zu %zu", sizeof(struct A), sizeof(struct B), sizeof(struct C), sizeof(struct D), offsetof(struct D, p));
  1. A. 13 13 8 21 13
  2. B. 24 16 8 24 16
  3. C. 24 16 12 32 24
  4. D. 24 16 12 24 16

Correct answer: D. 24 16 12 24 16

Each member is placed at a multiple of its own alignment and the struct is padded to a multiple of its strictest member. A: c@0, d@8, i@16, tail-padded to 24. B: d@0, i@8, c@12, padded to 16 – same members, reordered, 8 bytes smaller. C: c@0, s@2, d@4, i@8 → 12 (alignment 4). D: x occupies 0–11, t@12, then p must be 8-aligned → offset 16, size 24. The 13/13/8/21 option is the packed sum of member sizes.

More C Programming questions

Exams that ask this topic