locked
Marshal::SizeOf -- A bug ? RRS feed

  • Question


  • [StructLayout(Sequential)]
    ref class A
    {
    }

    [StructLayout(Sequential)]
    ref class B : public A
    {
    unsigned int i;
    }

    void main()
    {
    B^ b = gcnew B;
    Console::WriteLine(Marshal:Tongue TiedizeOf(b));}

    ------------------------

    What will this output to console?

    8 ! I can't believe!

    The same code in C# would output 4!

    I am using C++/CLI to map some C structs to .net class. But the strange behavior made it impossible to use inheritance in my code!

    Now I have to rewrite all my classes in C# to get the correct size.

    Is this a bug of C++/CLI, or it is intended?

    Thursday, September 27, 2007 3:19 PM

Answers

All replies

  • Hi: I can reproduce this and it does indeed seem like a bug in the C++ compiler. The problem is with the empty base-class A: the meta-data that the C++ compiler emits is slightly different to what the C# compiler emits. You should open a Connect issue for this and we'll take a look - though it is extremely unlikely that we'll be able to fix this for Visual C++ 2008.

     

    https://connect.microsoft.com/VisualStudio/feedback/CreateFeedbackForm.aspx?FeedbackFormConfigurationID=1160&FeedbackType=1&wa=wsignin1.0

     

    Thursday, September 27, 2007 5:54 PM
  •  The same thing happens while using Marshal class for PInvoke programming BluetoothGetRadioInfo.  With the cascaded/enclosed bluetooth_address within bluetooth_radio_info in C# structure, Marshal class return a bigger size for bluetooth_radio_info than it actually is.

    I hope this bug is resolved fast, because now it is the end of 2008.....
    Thursday, December 18, 2008 9:52 PM