是否需要在函数内遵守字节对齐? [英] Is it required to comply byte alignment within a function?

查看:94
本文介绍了是否需要在函数内遵守字节对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

这是我在这个小组的第一篇文章。很高兴见到你,很酷的家伙〜!


我正在各种嵌入式系统上进行系统编程,并且非常了解字节对齐问题。当我编写C代码时,特别是设计了一个结构,我注意成员的顺序和大小

变量。因为,我的老板总是说所有变量应该是4字节边界对齐,如果没有数据中止将发生在像ARM这样的特定机器上。


然而,我从未见过这样的情况。因为,我的编译器

会自动对齐边界,并在结构中的每个变量之间正确填充,如果需要的话。当然,打包选项

编译器可能会改变它的行为,这个假设可能很容易打破
。所以,我同意我的老板。


我想问的问题是他希望我在一个函数中对齐所有本地

变量。例如,请参阅以下代码。


案例#1 - 这不允许!!

int foo1()

{

char a;

int b;

short c;

...

}


案例#2 - 这没关系。好的。

int foo2()

{

char a;

char dummy [3];

int b;

短c;

...

}


我觉得这很麻烦甚至很荒谬。


您如何看待?

情况#1是否会导致数据中止或总线错误?

Hi all.
This is my first post on this group. Nice to meet you, cool guys~!

I''m on system programming on various embedded systems and understand
very well the byte alignment issues. When I write C code, especially
design a structure, I pay attention to the order and size of member
variables. Because, my boss always says that all variables should be
aligned by 4byte boundary, if not a data abort will be occurred on a
specific machine such like ARM.

However, I''ve never seen such situation. Because, my compiler
automatically aligns the boundary and gives padding properly between
each variable in the structure, if needed. Of course, pack option of
the compiler may change its behavior and this assumption may be break
easily. So, I agree with my boss.

The problem I want to ask is that he wants me to align all local
variables even in a function. For example, see the following code.

Case #1 - THIS IS NOT ALLOWED!!
int foo1()
{
char a;
int b;
short c;
...
}

Case #2 - THIS IS OK. GOOD.
int foo2()
{
char a;
char dummy[3];
int b;
short c;
...
}

I think this is bothersome and even ridiculous.

How do you think about?
Is the case #1 can make a data abort or bus error?

推荐答案

" gamja" <关于**** @ gmail.com>写道:
"gamja" <as****@gmail.com> wrote:
我想问的问题是他希望我甚至在函数中对齐所有局部变量。例如,请参阅以下代码。

案例#1 - 这不允许!!
int foo1()
{
char a;
int b;
短c;
......


案例#2 - 这没关系。好的。
int foo2()
{
char a;
char dummy [3];
int b;
短c;
...
}

我认为这很麻烦甚至荒谬。

您如何看待?


男人是个傻瓜,原因多于一个。

情况#1会导致数据中止或总线错误吗?
The problem I want to ask is that he wants me to align all local
variables even in a function. For example, see the following code.

Case #1 - THIS IS NOT ALLOWED!!
int foo1()
{
char a;
int b;
short c;
...
}

Case #2 - THIS IS OK. GOOD.
int foo2()
{
char a;
char dummy[3];
int b;
short c;
...
}

I think this is bothersome and even ridiculous.

How do you think about?
The man is a fool, and that for more reasons than one.
Is the case #1 can make a data abort or bus error?




如果确实如此,你不是在使用C编译器,而是使用bodge工作。


Richard



If it does, you''re not using a C compiler, but a bodge job.

Richard


我可以知道它为什么吗?嗯..我需要更多的技术信息

来说服我的老板。

May I know why it is? Hmm.. I need more information somewhat technical
to persuade my boss.


gamja< as **** @ gmail.com>写道:
gamja <as****@gmail.com> wrote:
我正在各种嵌入式系统上进行系统编程,并且非常了解字节对齐问题。当我编写C代码时,特别是设计一个结构,我要注意成员变量的顺序和大小。因为,我的老板总是说所有变量都应该按4字节边界对齐,否则会在像ARM这样的特定机器上发生数据中止。
然而,我从未见过这样的情况。因为,我的编译器会自动对齐边界,并在结构中的每个变量之间正确填充,如果需要的话。当然,编译器的打包选项可能会改变其行为,这个假设可能很容易破解。所以,我同意我的老板。
我想问的问题是他希望我甚至在函数中对齐所有局部变量。例如,请参阅以下代码。
案例#1 - 这是不允许的!!
int foo1()
{
char a;
int b;
短c;
...
}
案例#2 - 这没关系。好的。
int foo2()
{
char a;
char dummy [3];
int b;
短c;
...
}
我认为这很麻烦甚至荒谬。
I''m on system programming on various embedded systems and understand
very well the byte alignment issues. When I write C code, especially
design a structure, I pay attention to the order and size of member
variables. Because, my boss always says that all variables should be
aligned by 4byte boundary, if not a data abort will be occurred on a
specific machine such like ARM. However, I''ve never seen such situation. Because, my compiler
automatically aligns the boundary and gives padding properly between
each variable in the structure, if needed. Of course, pack option of
the compiler may change its behavior and this assumption may be break
easily. So, I agree with my boss. The problem I want to ask is that he wants me to align all local
variables even in a function. For example, see the following code. Case #1 - THIS IS NOT ALLOWED!!
int foo1()
{
char a;
int b;
short c;
...
} Case #2 - THIS IS OK. GOOD.
int foo2()
{
char a;
char dummy[3];
int b;
short c;
...
} I think this is bothersome and even ridiculous.




它是。如果你的C编译器不会生成将变量

放在具有正确对齐的地址的代码,那么编译器

就会被严重破坏。您的案例#1是完全合法的,并且每个和/或
每个编译器都有权将自己称为C编译器将

单独放入必要的填充或重新填充安排订单

的变量 - 没有要求''a''低于'b'b地址而不是'b''因为你定义' 'a''在''b''之前。所以,在某些架构上,你最终可能会将变量重新排序为例如

''b'','c'',''a''。 />

如果你的老板声称是真的,那么编写便携式程序几乎是不可能的。有许多架构具有

不同的对齐要求(例如,对于某些类型和机器

,您可能不得不在4字节边界上对齐,而是在8字节或

甚至可能是16字节边界)。如果你编写的程序

应该独立于一个特殊的架构那么你

将无法预先知道填充量是多少
需要
。这只是机器上的编译器知道你的b / b
程序最终将被编译并且_it_必须采取

来处理这个问题。


当然,有些编译器有(非标准)选项来否决这种行为(但据我所见,只有结构)。

只有当你坚持使用该选项时才会成为你的责任

来处理对齐问题,但是你要求它并且你需要给予b
$ b绳索挂起自己。但是只要你不使用

这个选项,结构的所有成员变量也将始终由编译器正确对齐。

问候,Jens

-

\ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de

\ __________________________ http://www.toerring.de



It is. If your C compiler won''t produce code that puts the variables
at addresses with proper alignment all by itself then that compiler
is horribly broken. Your case #1 is perfectly legal and each and
every compiler that has any right to call itself a C compiler will
put in the necessary padding all by itself or re-arrange the order
of the variables - there''s no requirement that ''a'' comes at lower
address than ''b'' just because you defined ''a'' before ''b''. So, on
some architectures you may end up with variables reordered to e.g.
''b'', ''c'', ''a''.

If what your boss claims would be true it would be nearly impossible
to write portable programs. There are lots of architectures with
different alignment requirements (e.g. for some types and machines
you may have to align not on 4-byte boundaries but on 8-byte or
maybe even 16-byte boundaries). And if you write programs that
are supposed to be independent of a special architecture then you
won''t be able to know in advance how much padding is going to be
needed. This is only known to the compiler on the machine your
program finally is going to be compiled on and _it_ has to take
care of this problem.

Of course, some compilers have an (non-standard) option to overrule
this behaviour (but only, as far as I have seen, for structures).
Only if you insist on using that optionit becomes your responsibility
to take care of alignment issues, but then you asked for it and you
get given enough rope to hang yourself. But as long as you don''t use
this option also all member variables of structures will always be
aligned properly by the compiler.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


这篇关于是否需要在函数内遵守字节对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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