Bug 2867 - Problems with packing, marshaling arrays in structs
Summary: Problems with packing, marshaling arrays in structs
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-01-12 11:44 UTC by viric
Modified: 2017-07-07 18:30 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description viric 2012-01-12 11:44:16 UTC
I've had troubles in linux-x86_64, when I was marshaling an object like this. In C:
typedef struct X_point3d
    /* array x, y, z */
    float p[3];
} X_point3d;

In .NET c#, I described:
    public struct X_point3d
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3,
                ArraySubType = UnmanagedType.R4)]
        public float[] p; /* In C, p[3] */

        public X_point3d(float x, float y, float z)
            this.p = new float[3];
            this.p[0] = x;
            this.p[1] = y;
            this.p[2] = z;

    static extern int suro_metric_to_amt(HandleRef met, int a, X_point3d centre,
        X_point3d axis, int b);


When calling suro_metric_to_amt, I checked the stack, and there were not 32-bit floats packed as 32-bit. There weren't also 64-bit floats packed as 64-bit. There were wrong numbers in the X_point3d, and they were also bad packed because 'b' also got a bad value.

I don't know much C#... maybe I'm doing something wrong? In windows, with visual studio, that code works perfectly in 32-bit.
Comment 1 Jeffrey Stedfast 2012-01-12 11:51:11 UTC
This is probably a compiler or runtime issue
Comment 2 Zoltan Varga 2012-01-12 18:24:05 UTC
Please attach a self contained test case.
Comment 3 Zoltan Varga 2012-01-15 20:37:19 UTC
Comment 4 Ludovic Henry 2017-07-07 18:30:13 UTC
If you can still reproduce with latest mono version, please feel free to reopen the bug. Thank you.