The Lost Art of C Structure Packing (2014)
1–10 of 116 posts
Re: The Lost Art of C Structure Packing (2014)
#2great bit! still, but how often is this going to be reposted?
https://hn.algolia.com/?query=The%20Lost%20Art%20of%20C%20St...
Re: The Lost Art of C Structure Packing (2014)
#3Ah, yes. BUS ERROR: http://blog.jgc.org/2007/04/debugging-solaris-bus-error-caus...
Re: The Lost Art of C Structure Packing (2014)
#4Very nice guide. I remember seeing extremely similar code when I was working with raw TCP packets and I always wondered what all the esoteric rules were for working with raw struct memory. There was no where that seemed to explain things happening on this level so it kind of just never made any sense to me. Guess now I can finally figure out what all that code meant, awesome.
Re: The Lost Art of C Structure Packing (2014)
#5Sigh... apparently this is now enough of rocket science to be considered for posting on HN...
People using packing pragma of GCC should also beware -- an access to a field of a packed structure will be done bytewise, whether a variable happens to be actually aligned or not (I guess the compiler simplified its life by assuming no variable is ever aligned in packet structs), so memory size would go down but CPU use might grow.
At least this used to be true a few years ago, haven't reverified recently.
Re: The Lost Art of C Structure Packing (2014)
#6It concerns me when a technique I've used this week is called a "lost art". I'd suggest putting a [NOOB] tag on this kind of post.
Except ESR wrote this one. So maybe [FAUX-NOOB].
Re: The Lost Art of C Structure Packing (2014)
#7See also previous discussions of this article from 2014 and 2015:
Re: The Lost Art of C Structure Packing (2014)
#8Last time I manually packed structures I was doing GPU programming. I forget the details, but the CPU and GPU had different alignment requirements so anything other than a manually packed structure broke in weird ways.
Re: The Lost Art of C Structure Packing (2014)
#9great bit! still, but how often is this going to be reposted? https://hn.algolia.com/?query=The%20Lost%20Art%20of%20C%20St...
Every time we'd like another round of "Lost art? Why, I packed me some C structs this morning over breakfast! You young whippersnappers!" comments from crusty old C dudes.
Re: The Lost Art of C Structure Packing (2014)
#10I compile with -Wpacked when I'm building something to serialize structs to disk. It helps. I know sizeof will return the value with the padding, but I'd rather do it myself.