命名联合内的数组元素或结构体和数组
考虑以下结构:
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 结构的开头。到目前为止还不错。但我似乎没有找到任何保证,如果您愿意的话,在 x
和 y
或 y
之间不会有空格或填充code> 和 z
等。不过我还没有检查过 C99 标准。
问题是,如果 Endpoint 结构元素之间有空格,那么这个想法就行不通。
问题:
我是否正确,确实不能保证这在 C 或 C++ 中都有效。
这实际上适用于任何已知的实现吗?换句话说,您知道有哪些实现无法实现此功能吗?
是否有任何标准(我的意思是不是特定于编译器的)方法来表达相同的想法?也许 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:
Am I right that there indeed is no guarantee that this will work either in C or 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?
By the way, this isn't something I am doing in production code, don't worry, just curious. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
.z()
而不仅仅是.z)
大多数编译器应该支持使用编译指示或属性来压缩结构。例如
#pragma pack
。.z()
instead of just.z
)Most compilers should support squashing a structure using a pragma or an attribute.
#pragma pack
for example.您可以通过引用数组的每个元素来避免任何内存对齐问题,只要您在类中的引用之前声明数组以确保它们指向有效数据即可。话虽如此,我怀疑对齐对于双打来说是一个问题,但对于其他类型可能是个问题(也许是在 64 位架构上浮动?)
希望这会有所帮助。
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?)
Hope this helps.
当涉及到该标准时,有两个问题:
话虽如此,实际上这适用于普通编译器。事实上,这种代码广泛传播,通常用于将一种类型的值重新解释为另一种类型的值。
When it comes to the standard, there are two problems with it:
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.
填充字节不会导致问题,因为所有变量都是 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 treatVector4D
as adouble
array. That means,v.Endpoint.z
is essentially the same asv[2]
.