In the K&R C programming code, the following code is given
typedef long Align; /* for alignment to long boundary */
union header { /* block header: */
struct {
union header *ptr; /* next block if on free list */
unsigned size; /* size of this block */
} s;
Align x; /* force alignment of blocks */
};
typedef union header Header;
Then it goes on to say that "The Align field is never used; it just forces each header to be aligned on a worst-case boundary". I have read the text multiple times but still do not understand why long is needed for alignment. SIZE is an int and PTR is 4 bytes, so shouldn't it be aligned anyways? And why use long, and not int? Thanks.
That's a very old book. At the time a pointer on many machines was only 16-bits, and a long was 32-bits, so the union forced alignment to 32-bits.
And as @Deduplicator points out, there are still many embedded systems that use 16-bit pointers.
Edit in response to the comment: The topic of alignment is fairly broad and full of nuance. To keep things simple, the discussion that follows makes these assumptions
int
is 16-bitslong
is 32-bitsThe following structure would occupy 32-bits
struct header {
struct header *ptr;
unsigned size;
};
However, the fields within the structure are still 16-bits each. Therefore, the alignment requirement is still only 16-bits. So for example, placing the structure at address 0xAA02 would be perfectly valid since ptr
would be at an aligned address (0xAA02) and size
would also be at an aligned address (0xAA04).
However, the address 0xAA02 is not suitably aligned for the union, since the x
field of the union requires 32-bit alignment. Thus, placing the structure into a union with x
forces the compiler to place the structure on a 4-byte boundary, when otherwise it could be placed on a 2-byte boundary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With