4 ms·
> Eventually, I had to learn to rely on the standard instead of folklore; to trust measurements and not presumptions... Indeed, testing your assumptions, becau
by zoomablemind 7y ago
> Eventually, I had to learn to rely on the standard instead of folklore; to trust measurements and not presumptions...
Indeed, testing your assumptions, because even a defined standard may result in a differing implementation of it. Especially in critical applications, testing the expectations gives some sense of a defined behavior.
This quizz is amusing as a mental exercise and a parable, but in reality all of these cases had to be fleshed out on a real platform, with real compiler and ... specified expectations of the behavior.
None of the cases in fact communicate a clear intent, except maybe #1 to figure out the padded size, still it's somewhat open-ended. Perhaps returning a specific condition (return sizeof(struct ...)==5; ) would show a clear intent. Not that it would change the right answer, just such a case may indeed be true on a specific platform, compile flags erc.
- zoomablemind 7y agoJust to flesh this out, I put the puzzle code through gcc 6.3, on ideone https://ideone.com/PziSzq https://ideone.com/PziSzq Result: 1:8 2:0 3:-96 4:1 5:2 Sure, there's a bunch of warnings emitted... fortunately.