Difficulty: Medium
Correct Answer: 12 bytes
Explanation:
Introduction / Context:
Struct size depends on the sizes of its fields and alignment rules. Reference-type fields inside a struct store only a reference (pointer), not the referenced object's inline data.
Given Data / Assumptions:
Concept / Approach:
Total size is the sum of field sizes plus any padding required to satisfy alignment. Here, the natural layout 4 + 4 + 4 = 12 requires no additional padding for 32-bit alignment, so the struct occupies 12 bytes.
Step-by-Step Solution:
Verification / Alternative check:
Use System.Runtime.InteropServices.Marshal.SizeOf(typeof(Sample)) in a 32-bit process to confirm 12. In a 64-bit process, the size would be 16 (pointer = 8), which is why the assumption matters.
Why Other Options Are Wrong:
Common Pitfalls:
Counting the in-memory size of the referenced object Trial inside the struct; only the reference is stored in the struct.
Final Answer:
12 bytes
Discussion & Comments