Live data from Hacker News

C Struct Layoutings

mostlynerdless.de

1–3 of 3 posts

Re: C Struct Layoutings

#2
(I'm the author) Every wondered how C data structures are layouted and aligned in memory? Every wondered how C data structures are layouted and aligned in memory? This is the topic of the newest blog post in my hello-ebpf series.

Re: C Struct Layoutings

#3
There are two main things that should be kept in mind: the native word size, and the endianness.

Quite often a lot of the struct is empty space, ferinstance you might have a 4-byte word. and you only want a single byte. Some systems will 'waste' 3 bytes, such that the byte in the struct is always at the beginning of the native 4-byte word access. And whether or not that happens, instead of packing as many bytes as possible together in a single 4-byte word maybe will mainly depend on your compiler.

Maybe I'm just too lazy, but I don't get hung up any more on how a particular struct is laid out in RAM, or whether that layout is wasteful or not. I just set it out as it suits me, and let the software and hardware work out what goes where for the best convenience of the system.

Many years ago, when I needed every byte I could get, I might have squeezed in every last byte. Not today. Not when we blithely throw whole gigabytes about.

As a curiosity, it might be worth spending 30 minutes over, but that's about it.