TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

C Struct Layoutings

2 pointsby parttimenerdabout 1 year ago

2 comments

simonblackabout 1 year ago
There are two main things that should be kept in mind: the native word size, and the endianness.<p>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 &#x27;waste&#x27; 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.<p>Maybe I&#x27;m just too lazy, but I don&#x27;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.<p>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.<p>As a curiosity, it might be worth spending 30 minutes over, but that&#x27;s about it.
parttimenerdabout 1 year ago
(I&#x27;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.