结构和联合对齐 [英] struct and union alignment

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

问题描述



我想检查一下我是否理解以下摘录:


6.2.5#26(类型):

所有指向结构类型的指针应具有相同的表示形式和

对齐要求。所有指向联合类型的指针都应该具有相同的表示和对齐要求。


这是否意味着* all * structure(或union)类型具有相同的

对齐方式?

例如。类型

struct {char c; }



struct {long double ldt [11]; }

有相同的对齐要求吗?

如果上面的答案是是,则意味着:


1.任何结构(联合)的对齐必须是最大值或其倍数。

(结构(联合)的对齐不能比限制更少
$ b $其成员的b [*]。由于任何(对象)类型可能是成员,因此在
中,至少有一个struct(union)必须至少具有该类型的对齐。

由于所有struct(union)类型具有相同的对齐方式,

因此所有必须满足最大值。)


2.对齐所有结构_and_联合的要求是相同的。

(因为你可以将一个结构作为一个联合的成员,一个联合作为一个结构的

成员,因此,它们的对齐方式必须相同。

[*]在Google搜索过程中,我了解到类型的对齐

可能在结构和外部有所不同。在这里,我的意思是对于特定架构必须满足的类型的限制性对齐要求最少



-

Stan Tobias

sed's / [AZ] // g''发送电子邮件

解决方案

< BLOCKQUOTE>" S.Tobias" < SN ******* @ amu.edu.pl>写道:

我想检查一下我是否理解以下摘录:

6.2.5#26(类型):
所有指向结构类型的指针都应该具有相同的表示和对齐要求。所有指向联合类型的指针都应具有相同的表示和对齐要求。

这是否意味着* all * structure(或union)类型具有相同的
对齐方式?
例如。类型
struct {char c; }

struct {long double ldt [11]; }
具有相同的对齐要求?




不,它指的是指针的对齐方式,而不是对齐方式
$ b结构或联合的$ b。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


2004年9月23日19:10:38 GMT

" S.Tobias" < SN ******* @ amu.edu.pl>写道:


我想检查一下我是否理解以下摘录:

6.2.5#26(类型):类型的指针应具有相同的表示和对齐要求。

这是否意味着* all * structure(或union)类型具有相同的<对齐?
例如。类型
struct {char c; }

struct {long double ldt [11]; }
具有相同的对齐要求?


是的。想想要实现指向

匿名结构的指针需要做些什么。当编译器正在编译一段代码时,

会增加一个指向匿名结构的指针,但它知道它是一个指向结构的指针,所以还有其他方法可以这已经完成了?

如果上面的答案是是,则表示:

1.任何结构(联合)的对齐必须最大或者它的多重。
(结构(联合)的对齐不能比它的成员[*]更少限制。因为任何(对象)类型可能是成员,在
至少有一个struct(union)必须至少具有该类型的对齐。
由于所有struct(union)类型具有相同的对齐方式,因此所有都必须满足最大值。 )


是。

2.所有结构_和_联合的对齐要求是相同的。
(因为你可以有一个结构作为成员一个联盟,一个联盟作为一个结构的成员,它遵循它们的对齐必须是相同的)。


对我来说听起来合乎逻辑。

[*]在谷歌搜索过程中我了解到类型的对齐
可能在结构上有所不同和外面。在这里,我的意思是对于特定架构必须满足的类型的最小限制对齐要求。




内部的对齐struct至少和外部一样严格,因为

你可以获取一个结构中元素的指针。但是,是的,我不能想到任何会阻止编译器填充

的东西。

-

Flash Gordon

有时我觉得拍摄对某些人来说太好了。

虽然我的电子邮件地址说垃圾邮件,但这是真实的我阅读它。


Keith Thompson< ks *** @ mib.org>写道:

" S.Tobias" < SN ******* @ amu.edu.pl>写道:

6.2.5#26(类型):
所有指向结构类型的指针应具有相同的表示和对齐要求。所有指向联合类型的指针都应具有相同的表示和对齐要求。

这是否意味着* all * structure(或union)类型具有相同的
对齐方式?


不,它指的是指针的对齐方式,而不是结构或联合的对齐方式。


有什么区别?


严格来说,(完整)_types_有对齐要求。

据我了解,当我们说指向TYPE的指针已经对齐

要求时,我们的意思完全相同:TYPE已经对齐

要求;我没有看到任何区分这两者的理由。


(我也考虑过指向结构的指针类型(即:struct s *)

对齐要求,但这在标准的引用部分

的背景下是没有意义的。)

-
Keith Thompson (The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。




-

Stan Tobias

sed's / [AZ ] // g''发送电子邮件



I would like to check if I understand the following excerpt correctly:

6.2.5#26 (Types):
All pointers to structure types shall have the same representation and
alignment requirements as each other. All pointers to union types shall
have the same representation and alignment requirements as each other.

Does it mean that *all* structure (or union) types have the same
alignment?
Eg. type
struct { char c; }
and
struct { long double ldt[11]; }
have the same alignment requirements?
If the answer to the above is "yes", then it means that:

1. Alignment of any struct (union) must be maximum or its multiple.
(Alignment of a struct (union) can''t be less restrictive than that
of its members[*]. Since any (object) type may be a member, at
least one struct (union) must have at least that type''s alignment.
Since all struct (union) types have the same alignment,
it follows that all must meet the the maximum one.)

2. Alignment requirements for all structs _and_ unions are the same.
(Since you can have a struct as a member of a union, and a union as a
member of a struct, it follows that their alignments must be the same).
[*] During google search I have learned that a type''s alignments
may differ in a struct and outside. Here I rather mean the least
restrictive alignment requirement for a type that has to be met
for a particular architecture.
--
Stan Tobias
sed ''s/[A-Z]//g'' to email

解决方案

"S.Tobias" <sN*******@amu.edu.pl> writes:

I would like to check if I understand the following excerpt correctly:

6.2.5#26 (Types):
All pointers to structure types shall have the same representation and
alignment requirements as each other. All pointers to union types shall
have the same representation and alignment requirements as each other.

Does it mean that *all* structure (or union) types have the same
alignment?
Eg. type
struct { char c; }
and
struct { long double ldt[11]; }
have the same alignment requirements?



No, it''s referring to the alignment of the pointer, not the alignment
of the struct or union.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


On 23 Sep 2004 19:10:38 GMT
"S.Tobias" <sN*******@amu.edu.pl> wrote:


I would like to check if I understand the following excerpt correctly:

6.2.5#26 (Types):
All pointers to structure types shall have the same representation
and alignment requirements as each other. All pointers to union
types shall have the same representation and alignment requirements
as each other.

Does it mean that *all* structure (or union) types have the same
alignment?
Eg. type
struct { char c; }
and
struct { long double ldt[11]; }
have the same alignment requirements?
Yes. Think about what what has to be done to implement pointers to
anonymous structs. When the compiler is compiling a piece of code that
increments a pointer to an anonymous struct all it knows is that it is a
pointer to a struct, so how else could this have been done?
If the answer to the above is "yes", then it means that:

1. Alignment of any struct (union) must be maximum or its multiple.
(Alignment of a struct (union) can''t be less restrictive than that
of its members[*]. Since any (object) type may be a member, at
least one struct (union) must have at least that type''s alignment.
Since all struct (union) types have the same alignment,
it follows that all must meet the the maximum one.)
Yes.
2. Alignment requirements for all structs _and_ unions are the same.
(Since you can have a struct as a member of a union, and a union as
a member of a struct, it follows that their alignments must be the
same).
Sounds logical to me.
[*] During google search I have learned that a type''s alignments
may differ in a struct and outside. Here I rather mean the least
restrictive alignment requirement for a type that has to be met
for a particular architecture.



The alignment inside a struct is at least as strict as outside, since
you can take a pointer to an element in a struct. However, yes, I
can''t think of anything that would prevent the compiler from padding
things out further.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.


Keith Thompson <ks***@mib.org> wrote:

"S.Tobias" <sN*******@amu.edu.pl> writes:

6.2.5#26 (Types):
All pointers to structure types shall have the same representation and
alignment requirements as each other. All pointers to union types shall
have the same representation and alignment requirements as each other.

Does it mean that *all* structure (or union) types have the same
alignment?

No, it''s referring to the alignment of the pointer, not the alignment
of the struct or union.
What''s the difference?

Strictly speaking, (complete) _types_ have alignment requirements.
As I understand it, when we say "a pointer to TYPE has alignment
requirements", we mean exactly same thing as: "TYPE has alignment
requirements"; I don''t see any reason to differentiate between these two.

(I have also considered "pointer to struct" type (ie.: struct s *)
alignment requirements, but this doesn''t make sense in context of
the quoted part of the Standard.)
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.



--
Stan Tobias
sed ''s/[A-Z]//g'' to email


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

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