指针分配不兼容的技术合法性

指针分配不兼容的技术合法性,第1张

概述C11标准ISO / IEC 9899:2011(E)规定了第6.5.16.1 / 1条中简单分配的以下限制: One of the following shall hold: the left operand has atomic, qualified, or unqualified arithmetic type, and the right has arithmetic type; the C11标准ISO / IEC 9899:2011(E)规定了第6.5.16.1 / 1条中简单分配的以下限制:

One of the following shall hold:

the left operand has atomic,qualifIEd,or unqualifIEd arithmetic type,and the right has
arithmetic type; the left operand has an atomic,or unqualifIEd version of a structure or union
type compatible with the type of the right; the left operand has atomic,or unqualifIEd pointer type,and (consIDering
the type the left operand would have after lvalue conversion) both operands are
pointers to qualifIEd or unqualifIEd versions of compatible types,and the type pointed
to by the left has all the qualifIErs of the type pointed to by the right; the left operand has atomic,and (consIDering
the type the left operand would have after lvalue conversion) one operand is a pointer
to an object type,and the other is a pointer to a qualifIEd or unqualifIEd version of
voID,and the type pointed to by the left has all the qualifIErs of the type pointed to
by the right; the left operand is an atomic,or unqualifIEd pointer,and the right is a null
pointer constant; or the left operand has type atomic,or unqualifIEd _Bool,and the right is a pointer.

我感兴趣的是双方都是不同于voID的不兼容类型的指针.如果我理解正确,这应该至少是调用UB,因为它违反了这个约束.不兼容类型的一个例子应该是(根据§6.2.7和§6.7.2)int和double.

因此,以下程序应违反:

int main(voID) {  int a = 17;  double* p;  p = &a;  (voID)p;}

gcc和clang都警告“-Win兼容指针类型”,但不要中止编译(使用-std = c11 -Wall -Wextra -pedantic进行编译).

类似地,以下程序只会导致“-Wint-conversion”警告,而编译就好了.

int main(voID) {  int a;  double* p;  p = a;  (voID)p;}

来自C,我预计这两个测试用例都需要一个转换来编译.是否有任何理由为什么任何一个方案将是标准合法的?或者,即使通过明确地使用-std = c11而不是-std = gnu11来禁用娱乐GNU C扩展名,支持此代码风格的至少有重要的历史原因?

解决方法 编译器标志(gcc和clang)要求检查严格的标准一致性并拒绝编译不合格的代码是-pedantic-errors:
$gcc -std=c11 -pedantic-errors x.cx.c: In function ‘main’:x.c:3:15: error: initialization from incompatible pointer type [-Wincompatible-pointer-types]   double* p = &a;               ^

铛:

$clang -std=c11 -pedantic-errors x.cx.c:3:11: error: incompatible pointer types initializing 'double *' with an      Expression of type 'int *' [-Werror,-Wincompatible-pointer-types]  double* p = &a;          ^   ~~1 error generated.

在野外的典型C代码的一个重要的比例(至少)是不合规的,所以这样的语义错误会导致大多数C程序和库无法编译.

总结

以上是内存溢出为你收集整理的指针分配不兼容的技术合法性全部内容,希望文章能够帮你解决指针分配不兼容的技术合法性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1250652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存