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:
在我看来,我在WinApi的IPAddress结构中看到了类似的东西。我的想法是让我可以通过索引和名称来使用数组组件,例如:
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.
在C ++标准中,保证在POD结构的开头不会有“空”空间,也就是说,元素x将位于Endpoint结构的beginnig中。目前很好。但是我似乎没有找到任何保证没有空的空间或填充,如果你愿意,在x和y之间,或者y和z之间等等。我还没有检查出C99标准。
The problem is that if there is an empty space between Endpoint struct elements, then the idea will not work.
问题是如果Endpoint结构元素之间有空的空间,那么这个想法将不起作用。
Questions:
-
Am I right that there indeed is no guarantee that this will work either in C or C++.
我是对的,确实无法保证这在C或C ++中都能正常工作。
-
Will this practically work on any known implementation? In other words, do you know of any implementation where this doesn't work?
这实际上适用于任何已知的实现吗?换句话说,你知道任何不起作用的实现吗?
-
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.
顺便说一句,这不是我在生产代码中所做的事情,不用担心,只是好奇。提前致谢。
4 个解决方案
#1
5
- yes
- depends on the alignment needs of the architecture and the compilers strategy
- 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 pack
for example.
大多数编译器应该支持使用pragma或属性压缩结构。例如#pragma pack。
#2
4
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.
希望这可以帮助。
#3
2
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.
未指定在写入联合中的元素并从另一个元素中读取时会发生什么,请参阅C标准6.2.6.1和K.1
该标准不保证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.
说到这一点,实际上这将适用于普通的编译器。事实上,这种代码广泛传播,通常用于将一种类型的值重新解释为另一种类型的值。
#4
0
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]
.
填充字节不会导致问题,因为所有变量都是double类型。编译器将Vector4D视为双数组。这意味着,v.Endpoint.z与v [2]基本相同。
#1
5
- yes
- depends on the alignment needs of the architecture and the compilers strategy
- 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 pack
for example.
大多数编译器应该支持使用pragma或属性压缩结构。例如#pragma pack。
#2
4
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.
希望这可以帮助。
#3
2
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.
未指定在写入联合中的元素并从另一个元素中读取时会发生什么,请参阅C标准6.2.6.1和K.1
该标准不保证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.
说到这一点,实际上这将适用于普通的编译器。事实上,这种代码广泛传播,通常用于将一种类型的值重新解释为另一种类型的值。
#4
0
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]
.
填充字节不会导致问题,因为所有变量都是double类型。编译器将Vector4D视为双数组。这意味着,v.Endpoint.z与v [2]基本相同。