Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8620693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:38:26+00:00 2026-06-12T06:38:26+00:00

I have several structs that have sequential layout: struct S1 { Guid id; }

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:38:28+00:00Added an answer on June 12, 2026 at 6:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that holds several vectors of objects: struct ComponentA { public:
I have a struct which has several arrays within it. The arrays have type
I have several HTML elements (buttons) that fire the same JQuery AJAX request. When
I have several Delphi programs that maintain connections to a database (some Oracle, some
I have several different numbers in a group that range in sizes and would
I have several xml files that are formated this way: <ROOT> <OBJECT> <identity> <id>123</id>
I have several variables that I need to send from page to page... What
I have several large code bases which compile into dynamic libraries. I know that
I have several small classes that are all peers of each other declared and
I have read several articles here and else where that it is OK to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.