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 8415241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:23:17+00:00 2026-06-10T01:23:17+00:00

public struct TestStruct { public int first; public int second; public int third; }

  • 0
public struct TestStruct
{
    public int first;
    public int second;
    public int third;
}

Marshal.SizeOf returns 12, which is what I assume because the ints are 4 bytes each. If I were to change third to a double instead of an int, I would expect Marshal.SizeOf to return 16. It does. But if I were to add a fourth that was a double, Marshal.SizeOf returns 24, when I expect 20. I could have 10 ints and it would end up each int counted as 4bytes each. But if I add a double in after 3 ints, the size isn’t what I expect.

public struct TestStruct //SizeOf 12
{
    public int first;
    public int second;
    public int third;
}  

public struct TestStruct //SizeOf 16
{
    public int first;
    public int second;
    public double third;
}  

public struct TestStruct //SizeOf 24, but I feel like it should be 20
{
    public int first;
    public int second;
    public double third;
    public int fourth;
}

Where has my thinking led me astray?

  • 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-10T01:23:19+00:00Added an answer on June 10, 2026 at 1:23 am

    In addition to JimR’s answer, you can explicitly change this behaviour using the StructLayout attribute:

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct TestStruct // sizeof is now 20
    {
        public int first;
        public int second;
        public double third;
        public int fourth;
    }
    

    You’ve told the compiler that this struct is sequentially laid out, and that the packing type should be 1, which means that all fields are laid out directly after each other. Pack 0 (the default) tells the compiler to use the current platforms padding.. while anything else (in multiples of 2) will lay it out in those specific boundaries. I would advise against this though.. it could become unpredictable depending on your usage.

    For more info, see this MSDN article: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.pack.aspx

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

Sidebar

Related Questions

Code to illustrate : public struct MyStruct { public int SomeNumber; } public string
Consider the following struct: public struct vip { string email; string name; int category;
I used a struct public struct stuff { public int ID; public int quan;
I have a struct like this: public struct stuff { public int ID; public
public struct customerItemListStruct { public int ID { get; set; } public string name
public struct PLU { public int ID { get; set; } public string name
Given the following code: public struct Foo { public Foo(int bar, int baz) :
Say I had a very simple struct in c# public struct foo{ public int
In ASP.NET C# I have a struct: public struct Data { public int item1;
I created a simple struct which consists of two value types. public struct Identifier

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.