Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marshal.SizeOf on a struct containing guid gives extra bytes

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!

like image 372
user1695516 Avatar asked Sep 24 '12 20:09

user1695516


1 Answers

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.

like image 103
Hans Passant Avatar answered Sep 23 '22 04:09

Hans Passant