On Mon, Mar 3, 2025 at 11:45 AM Steffen Nurpmeso <steffen@sdaoden.eu> wrote:
Warner Losh wrote in
 <CANCZdfrb7FLL_zhDq5E65kAzMsJ8HHBYJbcnWn+bhkAPUySKwg@mail.gmail.com>:
 |On Mon, Mar 3, 2025, 11:28 AM Larry McVoy <lm@mcvoy.com> wrote:
 |> 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?
 |>
 |
 |__attribute__ ((__packed__))

__attribute__((packed, aligned(1)))

I have forgotten why both.

packed (or __packed__) says no space between items.

aligned(1) means that the structure can start at any address.

Warner