24-bit int

kinke via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 2 00:20:07 PDT 2017


On Saturday, 2 September 2017 at 02:37:08 UTC, Mike Parker wrote:
> It's not a bug, but a feature. Data structure alignment is 
> important for efficient reads, so several languages (D, C, C++, 
> Ada, and more) will automatically pad structs so that they can 
> maintain specific byte alignments. On a 32-bit system, 4-byte 
> boundaries are the default. So a struct with 3 ubytes is going 
> to be padded with an extra byte at the end. Telling the 
> compiler to align on a 1-byte boundary (essentially disabling 
> alignment) will save you space, but will will generally cost 
> you cycles in accessing the data.

struct int24 {
     ubyte[3] _payload;
}

static assert(int24.sizeof == 3);
static assert(int24.alignof == 1);

Making absolute sense. ubytes don't need any specific alignment 
to be read efficiently.


More information about the Digitalmars-d-learn mailing list