结构布局问题 [英] structure layout question

查看:86
本文介绍了结构布局问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,


为什么C标准要求结构成员不被重新订购(6.2.5.20)?允许填充,并且取决于平台,

意味着无论如何都不能依赖于确切的布局,那么重点是什么?


没有这个限制编译器可以在

中以最有效的方式布局结构,以获得有效的一些定义。它会很容易用编译器特定的

pragma关闭这个重新排序,这通常用填充来完成。


-

Kyle A. York

Sr. Subordinate Grunt

Greetings,

Why does the C standard require the members of a structure not be
re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which
means one cannot rely on the exact layout anyway, so what''s the point?

Without this restriction the compiler could layout the structure in the
most efficient way possible, for some definition of efficient. It would
be easy enough to turn this reordering off with a compiler specific
pragma as is often done with padding.

--
Kyle A. York
Sr. Subordinate Grunt

推荐答案

在文章< ; 11 *************** @ sj-nntpcache-2.cisco.com>,

kyle york< ky *** @ cisco.comwrote:
In article <11***************@sj-nntpcache-2.cisco.com>,
kyle york <ky***@cisco.comwrote:

>为什么C标准要求结构的成员不被重新订购(6.2.5.20)?允许填充,并且依赖于平台,这意味着无论如何都不能依赖于确切的布局,那么重点是什么?
>Why does the C standard require the members of a structure not be
re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which
means one cannot rely on the exact layout anyway, so what''s the point?



我记得最好,不允许任意填充:只有

才能使条目对齐(对齐

规则取决于平台。)因此,如果您知道对齐规则

(例如)纯 double,那么你也知道对齐

规则为嵌入在struct中的double。


在任何情况下,取结构并用一个包裹它union,

其他成员是unsigned char数组。这是

a获得构成结构的字节的合法方式,而不是担心陷阱表示,有些情况下你可以b / b
构造对齐规则提供保证的地方

如果可以重新排序,则无法满足的无符号字符数组中的位置是什么?
例如,取一个已知后跟char的

成员的偏移量。 char是最常用的
一般对齐,所以你知道它紧跟在会员的

结尾之后。你知道成员的大小通过sizeof。

所以unsigned char数组索引成员的偏移量,

加上成员的大小,肯定会让你到了
的开头那个char - 但如果你重新排序了这些元素,那么char就可以相对于有问题的成员而言是


As best I recall, arbitrary padding is not permitted: only
where required to bring the entry into alignment (where alignment
rules are platform dependant.) So if you know the alignment rules
for (say) a "pure" double, then you also know the alignment
rules for a double embedded in a struct.

In any case, take the structure and wrap it around with a union,
the other member of which is an array of unsigned char. This is
a legal way to get at the bytes that make up the structure without
worrying about trap representations, and there are scenarios you
can construct where the alignment rules provide guarantees about
what will be where in the unsigned char array that wouldn''t be met
if reordering was possible. For example, take the offset of a
member that was known to be followed by a char. char is the most
general alignment, so you know it followed -immediately- after the
end of the member. You know the size of the member via sizeof.
So the unsigned char array indexed at the offset of the member,
plus the sizeof the member, is certain to get you to the beginning of
that char -- but if you''d reordered the elements, the char could
be anywhere relative to the member in question.


>没有这个限制,编译器可以以最有效的方式布局结构,以获得有效的定义。它很容易通过编译器特定的编译指示来关闭此重新排序,这通常是通过填充来完成的。
>Without this restriction the compiler could layout the structure in the
most efficient way possible, for some definition of efficient. It would
be easy enough to turn this reordering off with a compiler specific
pragma as is often done with padding.



编译器可以很容易地提供一个pragma到
pack有效,可能会破坏订购或可能需要

如果架构

不支持移动未对齐,则进出内部char缓冲区的移动缓慢。你可以把它称之为疯狂的东西,比如说......嗯,比如,#pragma pack

-

有些想法错了只有一个非常聪明的人才能相信他们。 - George Orwell

And it would be easy enough for a compiler to provide a pragma to
pack efficiently, possibly breaking ordering or possibly requiring
slow movements in and out of internal char buffers if the architecture
doesn''t support "move unaligned". You could call it something wild such
as ... ummm, say, #pragma pack
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell


kyle york写于03/01/07 13:06,:
kyle york wrote On 03/01/07 13:06,:

问候,

为什么C标准要求结构的成员不被重新订购(6.2.5.20)?允许填充,并且取决于平台,

意味着无论如何都不能依赖于确切的布局,那么重点是什么?


没有这个限制编译器可以在

中以最有效的方式布局结构,以获得有效的一些定义。它会很容易通过编译器特定的

pragma关闭此重新排序,这通常是通过填充来完成的。
Greetings,

Why does the C standard require the members of a structure not be
re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which
means one cannot rely on the exact layout anyway, so what''s the point?

Without this restriction the compiler could layout the structure in the
most efficient way possible, for some definition of efficient. It would
be easy enough to turn this reordering off with a compiler specific
pragma as is often done with padding.



结构的第一个元素必须首先出现,并且

之前必须没有填充。这允许将struct

指针转换为指向struct'的
第一个元素的指针,反之亦然,这是一个有用的属性。


在某些条件下,不同的结构类型

共享一个共同的初始子序列。元素

可以通过指向任一类型的指针访问,所以

长,因为访问是公共元素。那是

另一个有用的属性。


结构通常不只是一包相关元素,

但也已发布的描述格式。对于

示例,一个以幻数开头的图像文件

后跟版本号后跟......可能很好

由结构描述。有可移植性问题

这样的用法,但它们仍然有用。


-
Er ********* @sun.com


ro ****** @ ibd.nrc-cnrc.gc.ca (Walter Roberson)写道:


[在一个结构中]
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:

[in a struct]

我记得最好,不允许任意填充:只有

需要使条目对齐(其中对齐

规则取决于平台。)因此,如果您知道对齐规则

(例如)apure ; double,那么你也知道对于嵌入在struct中的double的对齐

规则。
As best I recall, arbitrary padding is not permitted: only
where required to bring the entry into alignment (where alignment
rules are platform dependant.) So if you know the alignment rules
for (say) a "pure" double, then you also know the alignment
rules for a double embedded in a struct.



这是一个很好的理论,但我看不出如何用标准的

报价来支持它。该标准的文本说There

可能是结构对象中未命名的填充,但不是它的开头的b / b $ b $。并且我没有看到任何限制。

-

我在我的DeathStation 9000上运行它并且恶魔飞出了我的鼻子。 --Kaz

This is a nice theory, but I can''t see how to back it up with a
quote from the standard. The text of the standard says "There
may be unnamed padding within a structure object, but not at its
beginning." and I don''t see any restrictions on that.
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz


这篇关于结构布局问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆