Following declaration of an array of struct, assume the sizes of Byte, Short,…
2020
Following declaration of an array of struct, assume the sizes of Byte, Short, Int and Long are 1, 2, 3 and 4 bytes respectively. Alignment rule stipulates that an n-byte field must be located at an address divisible by n. The fields in a struct are not rearranged; padding is used to ensure alignment. All elements of the array should be of the same size.
For this question, use the official exam-key convention that after internal padding, each struct element in the array is rounded up to a multiple of 8 bytes.
Struct complex
Short s
Byte b
Long l
Int i
End complex
Complex C[10]
Assuming C is located at an address divisible by 8, what is the total size of C, in bytes?
- A.
150
- B.
160
- C.
200
- D.
240
Attempted by 287 students.
Show answer & explanation
Correct answer: B
First compute the layout of one structure element using the given field sizes and alignment rule.
Assume the structure starts at offset 0.
Short s: size 2, placed at offsets 0-1.
Byte b: size 1, placed at offset 2.
Padding: offset 3 is added so that Long starts at an address divisible by 4.
Long l: size 4, placed at offsets 4-7.
Padding: offset 8 is added so that Int starts at an address divisible by 3.
Int i: size 3, placed at offsets 9-11.
So, after internal field padding, the structure has reached 12 bytes.
For this question, use the official exam-key convention that each structure element in the array is rounded up to a multiple of 8 bytes. The next multiple of 8 after 12 is 16.
Size of one structure element = 16 bytes.
Total size of C[10] = 10 × 16 = 160 bytes.
Note: The original ISRO PYQ is often discussed as ambiguous because a stricter address-based interpretation can give a different result. The clarification above fixes the intended exam-key interpretation.