命名联合内的数组元素或结构体和数组

发布于 2024-11-16 19:44:09 字数 880 浏览 2 评论 0原文

考虑以下结构:

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

在我看来,我在 WinApi 的 IPAddress 结构中看到了类似的东西。这个想法是让我能够通过索引和名称使用数组组件,例如:

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

在 C++ 标准中,保证 POD 结构的开头不会有“空”空间,即也就是说,元素 x 将位于 Endpoint 结构的开头。到目前为止还不错。但我似乎没有找到任何保证,如果您愿意的话,在 xyy 之间不会有空格或填充code> 和 z 等。不过我还没有检查过 C99 标准。

问题是,如果 Endpoint 结构元素之间有空格,那么这个想法就行不通。

问题:

  1. 我是否正确,确实不能保证这在 C 或 C++ 中都有效。

  2. 这实际上适用于任何已知的实现吗?换句话说,您知道有哪些实现无法实现此功能吗?

  3. 是否有任何标准(我的意思是不是特定于编译器的)方法来表达相同的想法?也许 C++0x 对齐功能可能会有所帮助?

顺便说一句,这不是我在生产代码中做的事情,别担心,只是好奇。提前致谢。

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. 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. 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.

The problem is that if there is an empty space between Endpoint struct elements, then the idea will not work.

Questions:

  1. Am I right that there indeed is no guarantee that this will work either in C or 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?

By the way, this isn't something I am doing in production code, don't worry, just curious. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拥抱没勇气 2024-11-23 19:44:09
  1. 取决于架构和编译器策略的对齐需求
  2. 否,但您可以制作一个对象包装器(但您最终会得到 .z() 而不仅仅是 .z)

大多数编译器应该支持使用编译指示或属性来压缩结构。例如#pragma pack

  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)

Most compilers should support squashing a structure using a pragma or an attribute. #pragma pack for example.

小耗子 2024-11-23 19:44:09

您可以通过引用数组的每个元素来避免任何内存对齐问题,只要您在类中的引用之前声明数组以确保它们指向有效数据即可。话虽如此,我怀疑对齐对于双打来说是一个问题,但对于其他类型可能是个问题(也许是在 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;
}

希望这会有所帮助。

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?)

#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.

深爱不及久伴 2024-11-23 19:44:09

当涉及到该标准时,有两个问题:

  • 未指定写入联合中的元素并从另一个元素读取时会发生什么,请参阅 C 标准 6.2.6.1 和 K.1
  • 该标准不保证布局结构体的布局与数组的布局相匹配,详细信息请参阅 C 标准 6.7.2.1.10。

话虽如此,实际上这适用于普通编译器。事实上,这种代码广泛传播,通常用于将一种类型的值重新解释为另一种类型的值。

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
  • 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.

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.

盗琴音 2024-11-23 19:44:09

填充字节不会导致问题,因为所有变量都是 double 类型。编译器会将 Vector4D 视为 double 数组。这意味着,v.Endpoint.z 本质上与 v[2] 相同。

Padding bytes will not cause an issue as all variables are of type double. The compiler will treat Vector4D as a double array. That means, v.Endpoint.z is essentially the same as v[2].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文