Re:Clang is better than GCC
by Stratoscope on Monday November 28, 2022 @12:57AM
(#33770248)
This was an interesting lesson for me. Not only did pretty much every commenter miss this point, but I did too!
We were all talking about better ways to use memcmp(), such as whether to use the "!= 0" or not, or my memeq() wrapper.
Those are all good things to consider if memcmp() is the right solution in the first place.
I only had the "aha" moment after I took a break for a long walk and dinner.
I should do that more often before assuming I have the right answer... :-)
Maybe this is an example of what they call an XY Problem:
Serializing structs directly is probably fine if you fully control the platform on both sides and you memset to 0 ahead of time (the latter probably only important if compressing or if you are worried about leaking data, which in a closed system you would typically not be...).
I agree whole-heartedly.
There is no situation in which `memcmp` should be used with structs. In code review this would have been flagged as an error, no matter how much the author cried "but but but ... there's no padding!".
Even if there is no padding right now (compiler flags, or struct has perfectly aligned fields), at some point in the future this code will be reused in a spot where there will be padding (compiler upgrade, someone adds a new field at the beginning of the struct, etc).
It's the same with serialising structs, you have to do it field by field.