In the .NET struct design guidelines, it gives the maximum sensible size of a struct as 16 bytes. How do you determine how large your struct is, and is it affected by the architecture your program is running on? Is this value 32-bit only, or for both archs?
Share
Yes, the size of a struct is affected by the architecture. C# structs in 32bit are aligned at 4 byte boundaries, and in 64bit they are 8 byte aligned.
Example:
Instances of this struct will take up 4 bytes in 32bit processes, and 8 bytes in 64bit processes, even though the “int bar” takes just 4 bytes on both 32bit and 64bit processes.
Update:
I did some testing with this. I wrote this code:
As a 64bit process, I get this output:
As a 32bit process, I get this output:
Observations: