结构和对齐 [英] Structs and Alignment
问题描述
如果我有
struct foo
{
char bar;
long baz ;
};
在许多平台上,填充和对齐正在进行以获得baz到
从偶数地址开始或4分的地址。所以baz不是
必然是一个字节(char的大小)到struct foo。
我怎么能准确知道struct foo baz中有多少字节?
/ David
If I have
struct foo
{
char bar;
long baz;
};
on many platforms, padding and alignment is taking place to get baz to
start at an even address or an address that 4 divides. So baz is not
necessarily one byte (the size of char) into struct foo.
How can I know exactly how many bytes into struct foo baz is?
/David
推荐答案
David Rasmussen写道:
David Rasmussen wrote:
如果我在许多平台上,填充和填充
结构foo
{
char bar;
long baz;
};
正在进行调整以使baz从偶数地址或4分频的地址开始。因此baz不一定是struct foo中的一个字节(char的大小)。
我怎样才能准确知道struct foo baz中有多少字节?
> / David
If I have
struct foo
{
char bar;
long baz;
};
on many platforms, padding and alignment is taking place to get baz to
start at an even address or an address that 4 divides. So baz is not
necessarily one byte (the size of char) into struct foo.
How can I know exactly how many bytes into struct foo baz is?
/David
使用< stddef.h>中的offsetof宏:
#include< stddef.h> ;
struct foo
{
char bar;
long baz;
};
int main(无效)
{
size_t baz_offset = offsetof(struct foo,baz) ;
返回0;
}
-Michael
-
电子邮件:我的是/ at / gmx / dot / de地址。
Use the offsetof macro from <stddef.h>:
#include <stddef.h>
struct foo
{
char bar;
long baz;
};
int main (void)
{
size_t baz_offset = offsetof(struct foo, baz);
return 0;
}
-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Pierre Maurette写道:
Pierre Maurette wrote:
David Rasmussenécrit:
David Rasmussen a écrit :
如果我有
结构foo
{
char bar;
long baz;
};
在很多平台上,填充和对齐都是为了让baz从偶数地址开始n地址为4分。所以baz不一定是一个字节(char的大小)到struct foo。
在我的平台上,你可以试着打赌:
- 没有头部填充,& struct ==& struct.firstfield(rule)。
- 每个字段根据编译器选项对齐,可能是它自己的大小。
- 结束填充,计算结果:
struct.firstfield正确对齐。
If I have
struct foo
{
char bar;
long baz;
};
on many platforms, padding and alignment is taking place to get baz to
start at an even address or an address that 4 divides. So baz is not
necessarily one byte (the size of char) into struct foo.
"on my platform", you can try to bet on:
- no head padding, &struct == &struct.firstfield (rule).
- each field aligned according the compiler options, maybe its own size.
- end padding, calculated for:
struct.firstfield correctly aligned.
不:这是计算得出的具有
严格对齐要求的结构成员在
中正确对齐了一个类型为struct foo的> = 2元素的数组。没有填充的结构数组(规则)。
是的。
注意:在C99中,具有灵活阵列成员的结构无法构建阵列。
所有这些都是递归的。当然。
Nope: it is calculated s.t. the structure member with the
strictest alignment requirements is correctly aligned in
an array of >=2 elements of type struct foo. array of structs without padding (rule).
Yep.
Note: In C99, structures with flexible array members cannot
build up arrays.
All this recursively, of course.
我怎样才能准确知道struct foo baz中有多少字节?
也许:
size_t pad =( char *)& foo.baz - & foo;
How can I know exactly how many bytes into struct foo baz is?
maybe:
size_t pad = (char*)&foo.baz - &foo;
非便携式。可以使用
offsetof宏来计算偏移量。
结果应该是不同的,例如你改变
long baz;
到
char baz;
Non-portable. The offset can be calculated using the
offsetof macro.
The result should be different if for example you change
long baz;
to
char baz;
s /应该/可能是/
干杯
迈克尔
-
电子邮件:我的是/ at / gmx / dot / de地址。
s/should be/is probably/
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
(取代< mn *********************** @ YOURBRAnoos.fr>)
David Rasmussen写道01/01/05:
(supersedes <mn***********************@YOURBRAnoos.fr>)
David Rasmussen wrote on 01/01/05 :
如果我有
结构foo
{
char bar;
long baz;
在许多平台上,正在进行填充和对齐以使baz从偶数地址或4分割的地址开始。所以baz不一定是struct foo中的一个字节(char的大小)。
我怎样才能准确知道struct foo baz中有多少字节?
If I have
struct foo
{
char bar;
long baz;
};
on many platforms, padding and alignment is taking place to get baz to
start at an even address or an address that 4 divides. So baz is not
necessarily one byte (the size of char) into struct foo.
How can I know exactly how many bytes into struct foo baz is?
>
大小:sizeof运算符是你的朋友(你的C-book也是如此)。
Offset:使用offsetof()宏。
-
Emmanuel
C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
C -library: http://www.dinkumware.com/refxc.html
显然你的代码不符合原始规格。
你用湿面条被判30鞭。
- alc ++中的Jerry Coffin
Size : The sizeof operator is your friend (so is your C-book).
Offset : Use the offsetof() macro.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
这篇关于结构和对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!