Results 1 to 5 of 5

Thread: why is sizeof() returning the wrong value

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    why is sizeof() returning the wrong value

    why does sizeof() on this structure return 6? shouldn't it be 5?

    [StructLayout(LayoutKind.Sequential)]
    struct PacketResponse
    {
    public byte a;
    public byte b;
    public short c;
    public byte d;
    };
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: why is sizeof() returning the wrong value

    It looks like after the four bytes the size will only go up in two-byte increments. It's nothing I've ever read about before but try declaring these types:
    Code:
    [StructLayout(LayoutKind.Sequential)]
    struct S1
    {
        public byte a;
    };
    
    [StructLayout(LayoutKind.Sequential)]
    struct S2
    {
        public byte a;
        public byte b;
    };
    
    [StructLayout(LayoutKind.Sequential)]
    struct S3
    {
        public byte a;
        public byte b;
        public short c;
    };
    
    [StructLayout(LayoutKind.Sequential)]
    struct S4
    {
        public byte a;
        public byte b;
        public short c;
        public byte d;
    };
    
    [StructLayout(LayoutKind.Sequential)]
    struct S5
    {
        public byte a;
        public byte b;
        public short c;
        public byte d;
        public byte e;
    };
    
    [StructLayout(LayoutKind.Sequential)]
    struct S6
    {
        public byte a;
        public byte b;
        public short c;
        public byte d;
        public byte e;
        public byte f;
    };
    
    [StructLayout(LayoutKind.Sequential)]
    struct S7
    {
        public byte a;
        public byte b;
        public short c;
        public byte d;
        public byte e;
        public byte f;
        public byte g;
    };
    and then run this code:
    CSharp Code:
    1. MessageBox.Show(Marshal.SizeOf(typeof(S1)).ToString(), "S1");
    2. MessageBox.Show(Marshal.SizeOf(typeof(S2)).ToString(), "S2");
    3. MessageBox.Show(Marshal.SizeOf(typeof(S3)).ToString(), "S3");
    4. MessageBox.Show(Marshal.SizeOf(typeof(S4)).ToString(), "S4");
    5. MessageBox.Show(Marshal.SizeOf(typeof(S5)).ToString(), "S5");
    6. MessageBox.Show(Marshal.SizeOf(typeof(S6)).ToString(), "S6");
    7. MessageBox.Show(Marshal.SizeOf(typeof(S7)).ToString(), "S7");
    You'll see that sizes 5 and 7 are skipped, and I'm guessing all odd numbers after that too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: why is sizeof() returning the wrong value

    Hmmm... just did a bit more testing and discovered that if you change the short to byte then odd numbered sizes are returned as you'd expect. There must be some sort of alignment issue causing padding to take place but I'm somewhat baffled.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: why is sizeof() returning the wrong value

    OK, I don't know exactly what all the issues and implications are but I just changed this:
    Code:
    [StructLayout(LayoutKind.Sequential)]
    which is implicitly the same as this:
    Code:
    [StructLayout(LayoutKind.Sequential, Pack=0)]
    to this:
    Code:
    [StructLayout(LayoutKind.Sequential, Pack=1)]
    and got a size of 5 for your original type. That means that the default packing pads a byte somewhere, while forcing a packing of 1 will not. I don't know whether overriding the default packing could cause an issue when passing your structure to unmanaged code or not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: why is sizeof() returning the wrong value

    Quote Originally Posted by jmcilhinney
    OK, I don't know exactly what all the issues and implications are but I just changed this:
    Code:
    [StructLayout(LayoutKind.Sequential)]
    which is implicitly the same as this:
    Code:
    [StructLayout(LayoutKind.Sequential, Pack=0)]
    to this:
    Code:
    [StructLayout(LayoutKind.Sequential, Pack=1)]
    and got a size of 5 for your original type. That means that the default packing pads a byte somewhere, while forcing a packing of 1 will not. I don't know whether overriding the default packing could cause an issue when passing your structure to unmanaged code or not.
    SWEET!
    thanks a lot man,... haha you know too much
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width