Forums - C# Corner

Forum guidelines
Mickey Marshall

Mickey Marshall

  • 689
  • 1.2k
  • 66.8k

getting the size of a struct in c# returns 2 more than c++

Feb 7 2016 2:06 PM

I have the following structure in c# and c++ :

c# - Marshal.SizeOf returns 20.
c++  sizeof returns 18
18 seems correct
why does c# return more? 

C#
        [StructLayout(LayoutKind.Sequential)]
        public struct WAVEFORMATEX
        {
            public short wFormatTag;
            public short nChannels;
            public int nSamplesPerSec;
            public int nAvgBytesPerSec;
            public short nBlockAlign;
            public short wBitsPerSample;
            public short cbSize;
        }

            wavFmt = new clsWinMMBase.WAVEFORMATEX();
     wavFmt.cbSize = (short)(Marshal.SizeOf(wavFmt));// returns 20

C++
typedef struct tWAVEFORMATEX
{
    WORD        wFormatTag;         /* format type */
    WORD        nChannels;          /* number of channels (i.e. mono, stereo...) */
    DWORD       nSamplesPerSec;     /* sample rate */
    DWORD       nAvgBytesPerSec;    /* for buffer estimation */
    WORD        nBlockAlign;        /* block size of data */
    WORD        wBitsPerSample;     /* number of bits per sample of mono data */
    WORD        cbSize;             /* the count in bytes of the size of */
                                    /* extra information (after cbSize) */
} WAVEFORMATEX
 WAVEFORMATEX m_stWFEX;
 m_stWFEX.cbSize=sizeof(WAVEFORMATEX);// returns 18

Why
 


Answers (2)