简体   繁体   English

C 标准:结构和联合说明符:“适当转换”的确切定义是什么?

[英]C standard: structure and union specifiers: what is the exact definition of 'suitably converted'?

N2479 C17..C2x working draft — February 5, 2020 ISO/IEC 9899:202x (E) (emphasis added): N2479 C17..C2x 工作草案 — 2020 年 2 月 5 日 ISO/IEC 9899:202x (E)(已添加重点):

6.7.2.1 Structure and union specifiers 6.7.2.1 结构和联合说明符

17 Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. 17 在结构 object 中,非位域成员和位域所在的单元的地址按声明顺序递增。 A pointer to a structure object, suitably converted , points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa.指向结构 object 的指针,经过适当转换,指向其初始成员(或者如果该成员是位字段,则指向它所在的单元),反之亦然。 There may be unnamed padding within a structure object, but not at its beginning.结构 object 中可能有未命名的填充,但不是在其开头。

18 The size of a union is sufficient to contain the largest of its members. 18 工会的规模足以容纳其最大的成员。 The value of at most one of the members can be stored in a union object at any time.任何时候最多可以将其中一个成员的值存储在联合 object 中。 A pointer to a union object, suitably converted , points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa.一个指向联合 object 的指针,经过适当转换,指向它的每个成员(或者如果成员是位字段,则指向它所在的单元),反之亦然。

Question: what is the exact definition of suitably converted ?问题: suitably converted的确切定义是什么?

Extra: if there is no exact definition of suitably converted , then shall the C implementation document its understanding?额外:如果没有suitably converted的确切定义,那么 C 实现文件是否应记录其理解? For example (C/C++ preprocessor domain), Microsoft understands the term single item (C++, N4713) as single, permanently indivisible preprocessor token (which leads to issues while porting code from gcc/clang/other, which has different understanding), however, it seems that they don't document their understanding of the single item .例如(C/C++ 预处理器域),Microsoft 将术语single item (C++,N4713)理解为single, permanently indivisible preprocessor token (这会导致从 gcc/clang/other 移植代码时出现问题,这有不同的理解),但是,似乎他们没有记录他们对single item的理解。

In this context "suitable converted" means converted to a proper compatible type.在这种情况下,“适当的转换”意味着转换为适当的兼容类型。 For example:例如:

#include <stdio.h>

struct mystruct {
    double a;
    int b;
};

int main()
{
    struct mystruct s = { 2.5, 4 };
    double *d = (double *)&s;
    printf("%f\n", *d);   // prints 2.500000
    return 0;
}

Here the first member of struct mystruct has type double .这里struct mystruct的第一个成员的类型double So a "suitable conversion" in this case means a struct mystruct * may be converted via explicit cast to double * and it will point to the a member.因此,在这种情况下,“适当的转换”意味着struct mystruct *可以通过显式强制转换转换为double *并且它将指向a成员。

This isn't a term with special meaning.这不是一个具有特殊含义的术语。

You are dealing with conversion from type A to type B. Type A is a pointer-to-structure type.您正在处理从类型 A 到类型 B 的转换。类型 A 是指向结构的类型。 Type B is not specified except that it must be suitable for use as a pointer to the initial member.除了必须适合用作指向初始成员的指针外,未指定类型 B。 Therefore, it must be a pointer (so we are dealing only with conversions between two pointer types) and it must follow the strict aliasing rule (the destination type can be pointer to a narrow character type, std::byte , the actual type of the first member, or a type that is representation compatible such as differing only in signedness).因此,它必须是一个指针(所以我们只处理两种指针类型之间的转换)并且它必须遵循严格的别名规则(目标类型可以是指向窄字符类型std::byte的指针,它的实际类型第一个成员,或表示兼容的类型,例如仅在符号上不同)。

Any pointer conversion that results in a suitable pointer satisfied "suitably converted".导致合适指针的任何指针转换都满足“适当转换”。

Question: what is the exact definition of suitably converted?问题:适当转换的确切定义是什么?

The C standard does not give any “exact definition” of “suitably converted.” C 标准没有给出“适当转换”的任何“确切定义”。

I interpret it to mean any sequence of conversions to a type of “pointer to type of initial member” or “pointer to type of structure” such that the specifications of the conversions ensure the final pointer is pointing to the appropriate address.我将其解释为对“指向初始成员类型的指针”或“指向结构类型的指针”类型的任何转换序列,以便转换规范确保最终指针指向适当的地址。 ( Eg , has not passed through a conversion with possibly incorrect alignment.) 例如,没有通过可能不正确的 alignment 的转换。)

Extra: if there is no exact definition of suitably converted, then shall the C implementation document its understanding?补充:如果没有适当转换的确切定义,那么C实现文件是否应该对其理解?

The C standard does not impose any requirement on a C implementation to document its understanding or interpretation of “suitably converted.” C 标准对 C 实施没有任何要求,以记录其对“适当转换”的理解或解释。

It's not a formal term, but between the lines we can tell that it is used to mean a valid pointer conversion.这不是一个正式的术语,但在字里行间我们可以看出它用于表示有效的指针转换。 And it needs to be carried out explicitly by the programmer by means of a cast.它需要由程序员通过强制转换显式执行。

In C, pretty much any object pointer can be converted to another object pointer casting.在 C 中,几乎任何 object 指针都可以转换为另一个 object 指针转换。 But what happens if you would de-reference such a pointer through the wrong type is a whole different story though.但是,如果您通过错误的类型取消引用这样的指针,会发生什么情况却完全不同。 Most of the time it's poorly-defined behavior.大多数情况下,这是定义不明确的行为。

Valid pointer conversions in this case:在这种情况下有效的指针转换:

  • A pointer to a type compatible with the type of the first member.指向与第一个成员的类型兼容的类型的指针。 1) 2) 3) 1) 2) 3)
  • A void pointer.一个空指针。 1) 2) 1) 2)
  • A pointer to character type.指向字符类型的指针。 2) 2)
  • A pointer to another structure type, where both structures are part of a union and share common initial member(s) of compatible type.指向另一种结构类型的指针,其中两个结构都是union的一部分并共享兼容类型的公共初始成员。 4) 4)

Optionally the pointer in any of the above cases can be type qualified.可选地,上述任何一种情况下的指针都可以是类型限定的。 Unless the first member is a qualified type, in which case the pointer needs to share all qualifiers of that type.除非第一个成员是限定类型,在这种情况下,指针需要共享该类型的所有限定符。 1) 3) 1) 3)

1) The rules of simple assignment 6.5.16.1. 1)简单赋值规则 6.5.16.1。
2) The rules regarding pointer conversions (6.3.2.3). 2)关于指针转换的规则(6.3.2.3)。
3) The rules of compatible type qualifiers (6.7.3). 3)兼容类型限定符的规则(6.7.3)。
4) The rule of common initial sequence (6.5.2.3). 4)共同初始序列规则(6.5.2.3)。

The common initial sequence one is an oddball rule which apparently got poor compiler support, but it's in line with "strict aliasing".常见的初始序列一是一个奇怪的规则,它显然得到了较差的编译器支持,但它符合“严格别名”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM