如何跨preT节6.3.2.3的C11标准的一部分,7? [英] How to interpret section 6.3.2.3 part 7 of the C11 standard?

查看:109
本文介绍了如何跨preT节6.3.2.3的C11标准的一部分,7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个上下文<一href=\"http://stackoverflow.com/questions/24002093/how-to-cast-the-address-of-a-pointer-generically-while-conforming-to-the-c-stand\">question有它是否被允许一些讨论(即会或不会引入实现定义或未定义行为)投 INT ** 无效** ,随后赋值给取消引用无效* 。这使我想起我的问题在 C11标准

In the context of another question there was some discussion on whether it was allowed (i.e. would or would not introduce implementation defined or undefined behavior) to cast int** to void** and subsequently assign a value to the dereferenced void*. This brings me to my question on the interpretation of the C11 standard

6.2.5(28)无效者具有相同重presentation和对齐要求为指针为字符类型的指针。 ...结果
  6.3.2.3(1)无效的指针可被转换成或从一个指针到任何对象类型。的指针的任何对象类型可被转换成一指向void,然后再返回;结果应比较等于原始指针。结果
  6.3.2.3(7)...当一指针的对象被转换为指针为字符类型,则结果点到最低处理的对象的字节。 ...

6.2.5 (28) A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. ...
6.3.2.3 (1) A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
6.3.2.3 (7) ... When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. ...

我的问题是,这是否

int* intptr = NULL;
void* dvoidptr = &intptr; /* 6.3.2.3 (1) */
*(void**)dvoidptr = malloc(sizeof *intptr); /* using 6.3.2.3 (1) */

与符合标准或不?我觉得奇怪,我,但我无法找到论据​​确凿线何乐而不为。 无效* 无效** 由6.3.2.3和6.2.5与6.3.2.3帮助,保证一起对齐。

conforms with the standard or not? It seems strange to me, but I cannot find a conclusive line of argument why not. void* to void** is guaranteed by 6.3.2.3 and 6.2.5 together with 6.3.2.3 help with the alignment.

推荐答案

code是无效的。

请参阅强调:

6.3.2.3(1)至无效的指针可被转换成或从一个指针到任何对象类型。一个指向任何对象类型可以被转换为指向void的,然后再返回;结果应比等于原来的指针

6.3.2.3 (1) A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

您正在转换 INT ** 无效* (罚款),然后以不同类型的无效** (也不行)。 C给出任何quarantee您可以安全地转换无效* 来比原来的类型(除了其他事情从转换为的char *

You are converting int** to void* (fine), and then to different type void** (not ok). C gives no quarantee that you can safely convert void* to anything other than the original type (aside from converting to char *)

此外, *(无效**)dvoidptr 的结果无效*当在现实中有 为int * 。如果,例如,的sizeof(void *的)== 2 的sizeof(INT *)== 1 ?你可以的 无效* 指针转换成其他类型的,但你不能的 reinter preT 的它直接作为其他类型的

In addition, *(void**)dvoidptr results in void* when in reality there is int*. What if, for example, sizeof(void*) == 2 and sizeof(int*) == 1? You can convert void* pointer to other type, but you cannot reinterpret it directly as other type.

这篇关于如何跨preT节6.3.2.3的C11标准的一部分,7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆