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.
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:
https://news.ycombinator.com/item?id=33762154
https://twitter.com/jfbastien/status/1592578174763745280?ref...
In general everything from the string.h header should be only used on non strings with great care.
Another variant is having x as an int. It is supposed to truncate the int on assignement, but instead it simply dumps the high bits in the alignement.
As the saying goes: Working code > crashing code > silent corruption.
The real problem here is not the bug in the memcmp() call, it's that you should not use a memory compare to compare two structs in a test at all.
If the structs have any padding, you are invoking undefined behavior if the padding is not initialized.
And as we've seen, memcmp() opens the door to all kinds of errors.
Don't rely on a compiler to catch this for you; instead write the code correctly in the first place.
The author's struct has x, y, and z fields, and the right way to compare these structs is to explicitly compare these three fields.
For example, based on the author's code (using a typedef for the struct, and naming it xyz instead of tmp):
Now you can use xyz_equal() in your test code and it won't have any of the problems that the misuse of memcmp() caused.