[英]C++ build warning : dereferencing type-punned pointer will break strict-aliasing rules
I have this warning on my cast line code: 我在强制转换代码中有此警告:
dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict- aliasing]
取消引用类型化指针将破坏严格别名规则[-Wstrict-alias]
unsigned char buffer[64];
...
unsigned int value = *(unsigned int*)buffer;
How to fix this warning ? 如何解决此警告?
Thanks for your help ! 谢谢你的帮助 !
What this code does depends on the platform's endianness, alignment rules, integer size, and other things. 该代码的作用取决于平台的字节序,对齐规则,整数大小以及其他因素。 There's no way to know what it does just by looking at it.
仅通过查看就无法知道它的作用。 But you probably want something like this:
但是您可能想要这样的东西:
unsigned int value = buffer[0];
value = (value << 8) | buffer[1];
value = (value << 8) | buffer[2];
value = (value << 8) | buffer[3];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.