@Greg is correct that one must write explicit comparison functions in the general case.
It is possible to use memcmp
if:
NaN
.-Wpadded
with clang to check this) OR the structs are explicitly initialized with memset
at initialization.BOOL
) that have distinct but equivalent values.Unless you are programming for embedded systems (or writing a library that might be used on them), I would not worry about some of the corner cases in the C standard. The near vs. far pointer distinction does not exist on any 32- or 64- bit device. No non-embedded system that I know of has multiple NULL
pointers.
Another option is to auto-generate the equality functions. If you lay your struct definitions out in a simple way, it is possible to use simple text processing to handle simple struct definitions. You can use libclang for the general case – since it uses the same frontend as Clang, it handles all corner cases correctly (barring bugs).
I have not seen such a code generation library. However, it appears relatively simple.
However, it is also the case that such generated equality functions would often do the wrong thing at application level. For example, should two UNICODE_STRING
structs in Windows be compared shallowly or deeply?