I understand that the align
attribute has a few different form of use.
In my first attempt, I was using it as follows:
align(1)
private struct TGAHeader
{
ubyte idLenght;
ubyte hasColormap;
ubyte imageType;
ushort cmFirstEntry;
ushort cmLength;
ubyte cmSize;
ushort xOrigin;
ushort yOrigin;
ushort width;
ushort height;
ubyte pixelDepth;
ubyte imageDescriptor;
}
// TGAHeader.sizeof == 20
Which resulted in the struct being padded with 2 extra unwanted bytes.
After changing it to:
private struct TGAHeader
{
align(1):
ubyte idLenght;
ubyte hasColormap;
ubyte imageType;
ushort cmFirstEntry;
ushort cmLength;
ubyte cmSize;
ushort xOrigin;
ushort yOrigin;
ushort width;
ushort height;
ubyte pixelDepth;
ubyte imageDescriptor;
}
// TGAHeader.sizeof == 18
I got the expected 18 bytes for the header size.
So my doubt is: What is the actual use of the first form of the align
attribute if it doesn't seem to align the data as one would expect?