On Mon, Mar 3, 2025 at 3:26 PM Steve Nickolas <usotsuki(a)buric.co> wrote:
On Mon, 3 Mar 2025, Larry McVoy wrote:
Does anyone know if gcc has an option to ignore
alignment and pack the
structs?
Isn't it __attribute__((packed)) or something like that?
There are a few different ways to do that; some compilers have
`#pragma pack(1)` to pack structs on byte boundaries. All are language
extensions.
Standard C, starting with (IIRC) C11, has the `_Alignas` keyword that
lets you specify extended alignment for data; this can be useful in a
struct or something. However, I just checked the latest C spec and
it's quite explicit that you can't use it to relax alignment
requirements to something less strict. That is, you can't portably say
`alignas(1)` for an `int`.
What Paul described from BLISS makes it sound like you could describe
things like an IPv4 packet (which has fields that start at odd bit
boundaries) exactly. That's neat.
However, usually when programming at _that_ low of a level in C or
Rust or something like that, you have an ABI you can rely on for
guarantees about the size and alignment of fundamental types. If I'm
porting to another machine, I have to be sure those assumptions hold
on the target, or update as needed. I guess that's a bummer.
Unix got away with being able to specify things the way it did for so
long because they controlled the horizontal and the vertical: the
compiler was symbiotic with the system. You knew that your C struct
looked like the layout of the hardware registers for some device
because you maintained the compiler in the same source tree. Those
days are long gone, of course, hence much of the current consternation
around portable C.
- Dan C.