[Mono-devel-list] Structure packing

Bernie Solomon bernard at ugsolutions.com
Wed Sep 10 21:04:07 EDT 2003

This is my take on this FWIW.

It can only pick one way of doing this assuming you don't specify
Pack in the StructLayoutAttribute which is a way you can control it
if you need to (and C compilers have their own ways too).

I believe it should just use the default alignments of the host platform
but this isn't quite what the current code does - for example on
Windows and linux the default alignments for doubles are
8 and 4 respectively. But the current code in marshal.c does the
same thing on both platforms. I did submit a patch for this
which has not been incorporated as yet (it also addresses
similar issue for 64 bit machines - but does not address
things if a platform had a natural alignment of ints of 2
which I am not sure if any that mono runs on does).

I also believe the combination of a DLL (x.dll/x.so say) built from this
without setting packing in the compiler from its default:

    #include <stddef.h>
    #include <stdio.h>

    struct s
        char c;
        double d;

    extern "C" __declspec(dllexport) void a(struct s *a)
        printf("%c %g - off %d\n", a->c, a->d, offsetof(struct s, d));

and a .EXE built from this C#:

    using System;
    using System.Runtime.InteropServices;

    class Test
        extern public static void a(ref Val v);

        public struct Val
            public byte c;
            public double d;

        static public void Main(String [] args)
            Val v;
            v.c = 0x63;
            v.d = 1.3;
            a (ref v);

should work "by default". It certainly does with MS's compilers.
I believe this won't work in the current windows mono build (I have
my patch in my version so it does work).

Anyway that's my feeling on what should happen here as it makes
having multi-platform C#/native code combinations the easiest
to handle but it isn't quite what does happen with the current

Bernie Solomon

----- Original Message ----- 
From: "Chris Seaton" <chris at chrisseaton.com>
To: <mono-devel-list at lists.ximian.com>
Sent: Wednesday, September 10, 2003 3:01 PM
Subject: [Mono-devel-list] Structure packing

> When passing a structure to a pinvoke how does mono deal with structure
> padding? I understand that there is no standard way of doing it, so when
> I use a structure with pinvoke mono must have to detect the padding
> method used somehow.
> -- 
> Chris Seaton
> chris at chrisseaton.com
> http://www.chrisseaton.com/
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list

More information about the Mono-devel-list mailing list