how to define a struct without padding

aki via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 24 18:25:44 PDT 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



More information about the Digitalmars-d-learn mailing list