On Mon, Mar 03, 2025 at 05:55:10PM +0000, segaloco via TUHS wrote:
> Truth be told the subjectivity of implementing struct memory characteristics has
> bewildered me more rather than less as time goes on.
Alignment is your answer. Understand that and the confusion goes away:
slovax ~/tmp cat pack.c
#include <stdio.h>
struct {
char a;
int b;
} foo;
int
main(void)
{
printf("%lu\n", sizeof(foo));
return (0);
}
slovax ~/tmp cc pack.c
slovax ~/tmp a.out
8
Even x86, it would appear, wants to do aligned loads. I'm a little
surprised by that though maybe I shouldn't be as there is a RISC
implemented by the microcode under the x86 CPU.
Does anyone know if gcc has an option to ignore alignment and pack the
structs?