C overcoming aliasing restrictions (unions?)

寵の児 提交于 2019-12-05 05:13:44

The compiler is correct as the aliasing rules are determined by the so-called 'effective type' of the object (ie memory location) you're accessing, regardless of any pointer magic. In this case, type-punning the pointers with a union is no different than an explicit cast - using the cast is actually preferable as the standard does not guarantee that arbitary pointer types have compatible representations, ie you're unnecessarily depending on implementation-defined behaviour.

If you want to conform to the standard, you need to copy the data to new variables or use a union during the declaration of the original variables.

If your 128-bit integers are either big-endian or little-endian (ie not mixed-endian), you could also use memcmp() (either directly or after negating the return value) or do a byte-wise comparison yourself: access through pointers of character type is an exception to the aliasing rule.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!