结构和对齐问题 [英] structures and alignment issues

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

问题描述

struct a

{

int b;

char a;

int c;

}


在i386上,这个结构的大小将是12个字节,因为3个字节是

在a之后填充,以便c对齐4字节边界。


所以我怀疑的是


1)只有当我坚持这个时,才会发挥可控性问题/>
结构在一个架构上(对于ex i386)并尝试在不同的架构上读取

结构(对于前摩托罗拉系列)

解决方案

6月14日上午8:30,sil ... @ gmail.com写道:


struct a

{

int b;

char a;

int c;


}


在i386上,这个结构的大小将是12个字节,因为3个字节是

填充之后所以c对齐4字节边界。


所以我怀疑的是


1)只有当我在一个架构上保留这个

结构(对于ex i386)并尝试阅读

时,才会发挥可能性问题结构回到不同的架构上(对于前摩托罗拉系列)



我想澄清的另一件事是做所有的编译器对齐

结构成员使用自然对齐或者这一切都不同

从建筑到建筑


si **** @ gmail.com 写道:


struct a

{

int b;

char a;

int c;

}


在i386上,此结构的大小为12个字节,因为3个字节是在b之后填充,因此c在4字节边界上对齐。



这是一种可能的对齐。


所以我怀疑的是


1)只有当我在一个体系结构上保留这个

结构(对于ex i386)并尝试阅读
$ b $时,才会发挥可能性问题b结构回到不同的架构上(对于前摩托罗拉系列)



或者使用不同编译器或相同编译器的相同架构

有不同的选择。或者......


-

Ian Collins。


si **** @ gmail.com 写道:


| 1)只有当我坚持

|时才会发挥可能性问题这个结构在一个架构上(对于ex i386)并尝试阅读

|结构回到不同的架构上(前摩托罗拉

|系列)


不,它总是一个问题。考虑单个架构上的单个编译器如何处理这种结构的差异

被告知要优化速度与被告知要优化大小...


-

Morris Dovey

DeSoto Solar

德索托,爱荷华州美国
http://www.iedu.com/DeSoto/

struct a
{
int b;
char a;
int c;
}

On i386 the sizeof this structure would be 12 bytes,as 3 bytes are
padded after a so that c is aligned on 4byte boundary.

So the doubts that i have is

1) Does the poratbility issue come into play only when i persist this
structure on one architecture ( for ex i386) and try to read the
structure back on a different architecture(for ex motorola series)

解决方案

On Jun 14, 8:30 am, sil...@gmail.com wrote:

struct a
{
int b;
char a;
int c;

}

On i386 the sizeof this structure would be 12 bytes,as 3 bytes are
padded after a so that c is aligned on 4byte boundary.

So the doubts that i have is

1) Does the poratbility issue come into play only when i persist this
structure on one architecture ( for ex i386) and try to read the
structure back on a different architecture(for ex motorola series)

One more thing i want to get clarified is do all the compilers align
structure members using natural alignement or does this all differ
from architecture to architecture


si****@gmail.com wrote:

struct a
{
int b;
char a;
int c;
}

On i386 the sizeof this structure would be 12 bytes,as 3 bytes are
padded after a so that c is aligned on 4byte boundary.

That''s one possible alignment.

So the doubts that i have is

1) Does the poratbility issue come into play only when i persist this
structure on one architecture ( for ex i386) and try to read the
structure back on a different architecture(for ex motorola series)

Or the same architecture with a different compiler, or the same compiler
with different options. Or...

--
Ian Collins.


si****@gmail.com wrote:

| 1) Does the poratbility issue come into play only when i persist
| this structure on one architecture ( for ex i386) and try to read
| the structure back on a different architecture(for ex motorola
| series)

No, it can always be an issue. Consider the differences in how a
single compiler on a single architecture might treat this structure
when told to optimize for speed vs when told to optimize for size...

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/


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

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