Thread overview
how to define a struct without padding
May 25, 2017
aki
May 25, 2017
Adam D. Ruppe
May 25, 2017
aki
May 25, 2017
Hi,

This is a code ported from some windows C program.
It is the Windows WAVE file header definition.
The field layout is the same with C,
but it has extra 2 bytes at the end,
sizeof(WAVEHEADER) in C is 46 while WAVEHEADER.sizeof in D is 48.
Why it still have padding in spite of "align(1)" ?
How can I remove extra padding bytes?

module main;
import std.stdio;
alias ushort WORD;
alias uint DWORD;

align(1) struct WAVEFORMATEX {
    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) */
};

align(1) struct WAVEHEADER {
    char[4]			szRIFF;
    int				lRIFFSize;
    char[4]			szWave;
    char[4]			szFmt;
    int				lFmtSize;
    WAVEFORMATEX		wfex;
    char[4]			szData;
    int				lDataSize;
};

void main() {
    writeln(WAVEHEADER.sizeof);	// 48; but it must be 46
}

Thanks,
Aki

May 25, 2017
On Thursday, 25 May 2017 at 01:25:44 UTC, aki wrote:
> align(1) struct WAVEFORMATEX {

align(1) on the outside removes padding from the end.

You also need `align(1):` on the inside to move all the padding to the end.


align(1) struct NAME {
   align(1):
      members
}

May 25, 2017
On Thursday, 25 May 2017 at 01:34:35 UTC, Adam D. Ruppe wrote:
> align(1) struct NAME {
>    align(1):
>       members
> }

My problem is resolved. Thank you for the quick reply.

Thanks,
Aki