size of structures in .NET

Posted by redpaladin on Stack Overflow See other posts from Stack Overflow or by redpaladin
Published on 2010-03-11T19:51:20Z Indexed on 2010/03/11 20:04 UTC
Read the original article Hit count: 174

Filed under:
|
|

My problem is very simple. My problem is to send a structure between a program in C to a C# program.

I made a struct in C#:

public struct NetPoint { 
    public float lat; // 4 bytes
    public float lon; // 4 bytes
    public int alt; // 4 bytes
    public long time; // 8 bytes
}

Total size of the struct must be 20 bytes.

When I do a sizeof() of this struct:

System.Diagnostics.Debug.WriteLine("SizeOf(NetPoint)=" +
               System.Runtime.InteropServices.Marshal.SizeOf(new NetPoint()));

The debug console shows: SizeOf(NetPoint)=24

But I expected to have 20 bytes. Why do I see a difference?

© Stack Overflow or respective owner

Related posts about struct

Related posts about interop