11

I have several structs that have sequential layout:

struct S1
{
  Guid id;
}

struct S2 
{
  Guid id;
  short s;
}

struct S3 
{
  Guid id;
  short s;
  short t;
}

Calling Marshal.SizeOf on above struct types, I got:

Size:
S1 = 16, as expected.
S2 = 20, copied an instance to a byte array, it only occupies first 18 bytes.
S3 = 20.

My question is that why the size of S2 is 20 but not 18. And this problem only comes up when Guid is in the struct.

Sorry can't find any useful info from msdn. I know Marshal.SizeOf gives the size of space the type will occupy in the memory, but I want to know why it deserves 2 extra bytes to make the size a multiple of 4.

And how can I avoid this "problem"?

Thanks a lot!

Tim M.
  • 53,671
  • 14
  • 120
  • 163
user1695516
  • 109
  • 5
  • Take a look at @Hans Passant's very thorough [answer](http://stackoverflow.com/a/3362736/1289454) on Structure memory layout. He claims that `Marhsal.SizeOf`can only provide a guesstimate. – gowansg Sep 24 '12 at 21:13

3 Answers3

10

This is because of the implied [StructLayout] attached to a struct, the Pack field is the important one. It dictates how members of the structure are aligned after getting marshaled. The default value for Pack is 8. Which says that any member less or equal to 8 bytes in size is aligned. In other words, a short will be aligned to an offset that's a multiple of 2, an int to 4, a long or double to 8.

Key point is that alignment should still work when an array of the structures is created. Easier demonstrated with a simpler example:

using System;
using System.Runtime.InteropServices;

class Program {
    static void Main(string[] args) {
        Console.WriteLine(Marshal.SizeOf(typeof(Example)));
        Console.ReadLine();
    }
}
struct Example {
    public int a;
    public short b;
}

Output: 8

The a member forces the size to be increased, two extra padding bytes are added to the structure to ensure that the int still aligns to an offset that's a multiple of 4 when the struct is used in an array. You can change the outcome by applying the attribute:

[StructLayout(LayoutKind.Sequential, Pack = 2)]
struct Example {
    public int a;
    public short b;
}

Output: 6

The a member will now be mis-aligned when the struct is used in an array.

Back to Guid, you can't see it from the declaration but it is internally made up of a number of members. The first one is private field named _a and it is an int. The Guid therefore requires a 4 byte alignment. So 2 extra padding bytes are needed to get your struct to align properly when it is used in an array. You'd need Pack=1 or Pack=2 on S2 to get 18 bytes.

More background on structs and the special way they are treated in .NET in this answer.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

I don't see you using a StructLayoutAttribute on your struct definitions. Therefore you won't know how they are "laid out". How can you tell they have sequential layout?

Some sources: sizeof() structures not known. Why? and When should I explicitly specify a StructLayout?

Community
  • 1
  • 1
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
0

I believe it is because some items must be aligned in memory to 4 byte (or 8 byte) boundaries. Therefore the struct is made a multiple of this size so that arrays of the structs will all be correctly aligned. On a 64-bit machine including a string forces it to be a multiple of 8 bytes; GUID seems to only require a multiple of 4 bytes even on 64-bits. If the struct containing a GUID was not a multiple of 4 bytes in size some of the GUIDs would not be on a 4-byte boundary in an array of these structs. I don't think you can avoid the 'problem' - why is it a problem?

Stuart Whitehouse
  • 1,421
  • 18
  • 30