News for hackers. Stuff that matters.
Re:The Lost Art of Structure Packing (2018) by gpderetta on Monday April 27, 2020 @02:21PM (#22998447)
Well, the contents of the padding bits is legally observable (with a memcpy for example) so the compiler can't assume they are unovservable.
Re:The Lost Art of Structure Packing (2018) by saagarjha on Monday April 27, 2020 @02:28PM (#22998508)
As far as I understand, padding bits are indeterminate until you observe them via a memcpy into a buffer, at which point they will collapse (only in the buffer) to some arbitrary but now-constant value. Is this correct?
Re:The Lost Art of Structure Packing (2018) by gpderetta on Monday April 27, 2020 @08:04PM (#23001759)
I think you are right in the general case. Definitely padding bits are not preserved during copies, but I think that at least in some cases they might be preserved, at least I think that if you memset+memcpy, the memcpy is guaranteed to see the zeros (what if you want to end the lifetime of the object and simply reuse the storage as an array of chars?). But there is divergence between C and C++ and also an area quite in flux (the notion of lifetimes of objects and memory locations gets tweaked every standard in C++, not sure where we are at now).

edit: interestingly, Biriba is yet another game in the canasta family, my theory is that different groups were originally playing these differents variants, but when they gained knowledge of the more popular variant (burraco), they started playing it but kept calling it with the original name. I guess that up until the internet era, these games were mostly passed via oral knowledge in casual groups.

edit2: macchiavelli [1] is another very fun game of the same family, but a lot more puzzle solving oriented.

[1] https://en.wikipedia.org/wiki/Machiavelli_(Italian_card_game...

Re:The Lost Art of Structure Packing (2018) by saagarjha on Monday April 27, 2020 @09:44PM (#23002423)
I looked it up for C, and as far as I can tell this has not standardized. (The Defect Report I read, which seems to have the latest opinion, literally calls such things "wobbly values". I'm not joking.) The TL;DR of Defect Report #451 seems to be that this is needs some work from the standard. In the case of an automatic struct or whatever (so you can't pull the bytes out from under it and reuse it) I think the consensus seems to be tending towards Heisenburg-ish values that have values that can change between reads, and certain library functions will be legal to call on them so that they "collapse" the state in the view that you've just created. For your case, I would have the question of whether two objects (e.g. a char array and a struct carved from that array) can share the same memory; in this case I would intuitively tend to agree with you that the padding bits are visible from the longer-lived object, kind of like an ad-hoc union. But I am not a licensed language lawyer, so I am not quite sure on that point.

I appreciate your edits, by the way ;)

Re:The Lost Art of Structure Packing (2018) by gpderetta on Tuesday April 28, 2020 @03:23AM (#23004398)
Thanks for hunting down the details.

And it seem that I should double check which comment I'm editing before submitting :)