简体   繁体   English

在联盟中命名数组元素或结构和数组

[英]Naming Array Elements, or Struct And Array Within a Union

Consider the following struct: 考虑以下结构:

struct Vector4D
{
   union
   {
      double components[4];
      struct { double x, y, z, t; } Endpoint;
   };
};

It seems to me that I have seen something similar in WinApi's IPAddress struct. 在我看来,我在WinApi的IPAddress结构中看到了类似的东西。 The idea is to give me the possibility to use the array components both by index and by name, for example: 我的想法是让我可以通过索引和名称来使用数组组件,例如:

Vector4D v;
v.components[2] = 3.0;
ASSERT(v.Endpoint.z == 3.0) //let's ignore precision issues for now

In the C++ standard there is a guarantee that there will be no "empty" space at the beginning of a POD-struct, that is, the element x will be situated right in the beginnig of the Endpoint struct. 在C ++标准中,保证在POD结构的开头不会有“空”空间,也就是说,元素x将位于Endpoint结构的beginnig中。 Good so far. 目前很好。 But I don't seem to find any guarantees that there will be no empty space or padding, if you will, between x and y , or y and z , etc. I haven't checked out the C99 standard though. 但是我似乎没有找到任何保证没有空的空间或填充,如果你愿意,在xy之间,或者yz等等。我还没有检查出C99标准。

The problem is that if there is an empty space between Endpoint struct elements, then the idea will not work. 问题是如果Endpoint结构元素之间有空格,那么这个想法就行不通了。

Questions: 问题:

  1. Am I right that there indeed is no guarantee that this will work either in C or C++. 我是对的,确实无法保证这在C或C ++中都能正常工作。

  2. Will this practically work on any known implementation? 这实际上适用于任何已知的实现吗? In other words, do you know of any implementation where this doesn't work? 换句话说,你知道任何不起作用的实现吗?

  3. Is there any standard(I mean not compiler-specific) way to express the same idea? 是否有任何标准(我的意思是不是编译器特定的)表达相同想法的方式? Maybe the C++0x alignment features might help? 也许C ++ 0x对齐功能可能会有所帮助?

By the way, this isn't something I am doing in production code, don't worry, just curious. 顺便说一下,这不是我在生产代码中所做的事情,不用担心,只是好奇。 Thanks in advance. 提前致谢。

  1. yes
  2. depends on the alignment needs of the architecture and the compilers strategy 取决于架构和编译器策略的对齐需求
  3. no, but you could make a object wrapper (but you will end up with .z() instead of just .z ) 不,但你可以制作一个对象包装器(但你最终会得到.z()而不仅仅是.z

Most compilers should support squashing a structure using a pragma or an attribute. 大多数编译器应该支持使用pragma或属性压缩结构。 #pragma pack for example. 例如#pragma pack

You can circumvent any memory alignment issues by having references to each element of the array, as long as you declare the array before the references in the class to ensure they point to valid data. 只要在类中的引用之前声明数组以确保它们指向有效数据,就可以通过引用数组的每个元素来规避任何内存对齐问题。 Having said that I doubt alignment would be an issue with doubles, but could be for other types (float on 64bit arch perhaps?) 话虽如此,我怀疑对齐是双打的问题,但可能是其他类型(浮在64位拱上?)

#include <iostream>
using namespace std;

struct Vector4D
{
    Vector4D() : components(), x(components[0]), y(components[1]), z(components[2]), t(components[3]) { }

    double components[4];

    double& x;
    double& y;
    double& z;
    double& t;
};

int main()
{
    Vector4D v;

    v.components[0] = 3.0;
    v.components[1] = 1.0;
    v.components[2] = 4.0;
    v.components[3] = 15.0;

    cout << v.x << endl;
    cout << v.y << endl;
    cout << v.z << endl;
    cout << v.t << endl;
}

Hope this helps. 希望这可以帮助。

When it comes to the standard, there are two problems with it: 说到标准,有两个问题:

  • It is unspecified what happens when writing to an element in a union and reading from another, see the C standard 6.2.6.1 and K.1 未指定在写入联合中的元素并从另一个元素中读取时会发生什么,请参阅C标准6.2.6.1和K.1
  • The standard does not guarantee the layout of the struct match that of the layout of the array, see the C standard 6.7.2.1.10 for details. 该标准不保证struct的布局与数组布局的布局相匹配,详见C标准6.7.2.1.10。

Having said this, in practice this will work on normal compilers. 说到这一点,实际上这将适用于普通的编译器。 In fact, this kind of code is widely spread and is often used to reinterpret values of one type into values of another type. 事实上,这种代码广泛传播,通常用于将一种类型的值重新解释为另一种类型的值。

Padding bytes will not cause an issue as all variables are of type double . 填充字节不会导致问题,因为所有变量都是double类型。 The compiler will treat Vector4D as a double array. 编译器将Vector4D视为double数组。 That means, v.Endpoint.z is essentially the same as v[2] . 这意味着, v.Endpoint.zv[2]基本相同。

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

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