On Thu Jan 18 13:28:39 2024 +0000, Yuxuan Shui wrote:
I would argue using `offsetof` to allocate is the bad practice here. Because it needs the assert there to be correct (what if you forget?), whereas `sizeof + sizeof` is always undefined behavior free. And the assert there is bad for portability. And there are places where there are missing asserts: https://gitlab.winehq.org/wine/wine/-/blob/master/dlls/dmsynth/synth.c?ref_t...
Missing asserts should definitely be added. Yes you can forget, as you can make a typo in the sizeof. But forgetting the assert might not be as bad as a sizeof typo.
Once they are there things are statically checked and therefore better than any other way which requires manual review to check that the types are truly the same, and to do it again every time it is changed.
I don't see a portability issue here, C_ASSERT is portable and uses the standard C _Static_assert whenever available.