本文介绍了严格走样C或C ++的事情吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ISO / IEC 9899:TC2,标准说,以下

So, it is not clear from the standard that a pointer of one type can be casted to pointer of another type.

解决方案

Strict aliasing rule is defined somewhere else. This is the wording:

C (ISO/IEC 9899:1999 6.5/7):

C++ (ISO/IEC 14882:2011 3.10 [basicl.lval] / 15):


The C standard doesn't prohibit you from casting the pointer to an unrelated type, provided there are no allignment problems. However, due to the strict aliasing rule, you basically can't dereference a pointer obtained from such a cast. So the only useful thing to do with such "invalid" pointer is to cast it back to the correct type (or a compatible type).

It's mostly the same in C++ with reinterpret_cast (5.2.10 [expr.reinterpret.cast] / 7):

这篇关于严格走样C或C ++的事情吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:46
查看更多