结构填充。 [英] Structure padding.

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

问题描述

伙计,


这个结构填充问题现在困扰着我,可能

没有找到一个满意的答案,所以这里......


我有一个结构,如下所示:

typedef struct {

int flag;

char键[3] [9];

char填充[??];

} foo;


我需要在这里填写填充数组的大小,以便

使整个结构大小为8的倍数。是否有我可以计算填充的
a方式?这个结构

将在AIX 5. *和RHL上使用。


提前致谢。


干杯,

Amar


-

如果你不想成功,没有人会试图阻止你。

Folks,

This structure padding issue is bothering me now, could
not locate a satisfactory answer on clc, so here it goes...

I have a structure, given below:
typedef struct {
int flag;
char keys[3][9];
char padding[??];
} foo;

I need to fill in the size of padding array here so as to
make the entire structure size a multiple of 8. Is there
a way in which I can calculate the padding ? This structure
will be used on AIX 5.* and RHL.

Thanks in advance.

Cheers,
Amar

--
If you don''t want to succeed, nobody will try to stop you.

推荐答案



2004年6月21日星期一,Amarendra写道:

On Mon, 21 Jun 2004, Amarendra wrote:

这个结构填充问题现在困扰着我,可能无法在clc上找到满意的答案,所以这里就是......

我有一个结构,如下所示:
typedef struct {
int flag;
char keys [3] [9];
char padding [??];
} foo;

我需要在这里填写填充数组的大小,以便使整个结构大小为8的倍数。是否有一种方法可以计算填充?

This structure padding issue is bothering me now, could
not locate a satisfactory answer on clc, so here it goes...

I have a structure, given below:
typedef struct {
int flag;
char keys[3][9];
char padding[??];
} foo;

I need to fill in the size of padding array here so as to
make the entire structure size a multiple of 8. Is there
a way in which I can calculate the padding ?




显然不是。任何编译器都能完全填充

你的结构使它成为8个字节的倍数,减去1,并且

你无能为力,从标准的观点

的观点。

在实践中,你可能会得到一个

系统 - 具体解决方案如


#define PAD_EIGHT(siz)((siz) - (siz)/ 8 * 8)

struct half_foo {

int flag;

char keys [3] [9];

};

typedef struct {

int flag;

char keys [3] [9];

char padding [PAD_EIGHT(sizeof(struct half_foo))];

} foo;


或(对于下一个人来说更便携但更清晰)


typedef struct {

int flag;

char keys [3] [9];

char padding [1]; / *填充到8个字节的倍数* /

} foo;


实际上,当然,根本不重要的是什么大小
你的结构
是:让计算机担心它。大多数

编译器都有某种优化速度/空间。标志

这将使你的代码快速运行,无论是什么大小的

一些小结构。


-Arthur



Obviously not. Any compiler is perfectly capable of padding
your structure to make it a multiple of 8 bytes, minus 1, and
there''s nothing you can do about it, from the Standard''s point
of view.
In practice, you''ll probably be able to get away with a
system-specific solution such as

#define PAD_EIGHT(siz) ((siz) - (siz)/8*8)
struct half_foo {
int flag;
char keys[3][9];
};
typedef struct {
int flag;
char keys[3][9];
char padding[PAD_EIGHT(sizeof (struct half_foo))];
} foo;

or (even less portably but much clearer to the next guy)

typedef struct {
int flag;
char keys[3][9];
char padding[1]; /* Pad to a multiple of 8 bytes */
} foo;

In practice, of course, it doesn''t matter at all what the size
of your structure is: let the computer worry about it. Most
compilers have some kind of "optimize for speed/space" flag
that will make your code run quickly no matter what the size of
some little structure is.

-Arthur


Amarendra< co ******** @ gmail.com>写道:
Amarendra <co********@gmail.com> wrote:
这个结构填充问题现在困扰着我,可能无法在clc上找到满意的答案,所以在这里... ... b $ b我有一个结构,如下所示:
typedef struct {
int flag;
char keys [3] [9];
char padding [??];
} foo;
我需要在这里填写填充数组的大小,以便使整个结构大小为8的倍数。是否有一种方法可以计算填充?这个结构将在AIX 5. *和RHL上使用。
This structure padding issue is bothering me now, could
not locate a satisfactory answer on clc, so here it goes... I have a structure, given below:
typedef struct {
int flag;
char keys[3][9];
char padding[??];
} foo; I need to fill in the size of padding array here so as to
make the entire structure size a multiple of 8. Is there
a way in which I can calculate the padding ? This structure
will be used on AIX 5.* and RHL.




这是clc无法回答的,因为没有要求<关于编译器应该使用C标准填充多少填充量的


显然不同架构之间会有所不同,但是

编译器和也许甚至不同的编译器版本想到的第一个

问题是为什么你需要结构尺寸为

是8的倍数?


< ; OT>

您可以编写一个脚本来编写创建结构的C程序

,如上所述,填充数组的长度不断增加,并且

然后写出结构的大小。该脚本编译

并运行这些程序,直到结构大小为8

的倍数,并在实际程序中使用填充数组的长度(可能是

将值写入包含在真实

程序中的包含文件中。对于一个相当短的剧本,不应该太难。

< / OT>

问候,Jens

-

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

\ __________________________ http://www.toerring.de


文章< 1d ************** ************@posting.google.com>

Amarendra< co ******** @ gmail.com>写道:
In article <1d**************************@posting.google.com >
Amarendra <co********@gmail.com> writes:
这个结构填充问题现在困扰着我,可能无法在clc上找到满意的答案,所以这里就是......

我有一个结构,如下所示:
typedef struct {
int flag;
char keys [3] [9];
char padding [??];
} foo;

我需要在这里填写填充数组的大小,以便使整个结构大小为8的倍数。是否有一种方法可以计算出填充? ...
This structure padding issue is bothering me now, could
not locate a satisfactory answer on clc, so here it goes...

I have a structure, given below:
typedef struct {
int flag;
char keys[3][9];
char padding[??];
} foo;

I need to fill in the size of padding array here so as to
make the entire structure size a multiple of 8. Is there
a way in which I can calculate the padding ? ...




答案是否定的 - 是的。


无法做到这一点一次通过,就像它一样,但是你可以通过在运行时生成声明然后(在这个运行时已完成的
之后)继续执行它,然后使用该声明之后

编译时间。


这确实做了一些假设,比如稍后调用

编译器会使用与早期调用

编译器相同的布局 - 除非你等待太长时间(因为

,编译器已被更高版本替换),否则应该是真的,或者更改

你传递给的标志集编译器。它还使你的软件的构建过程变得更加复杂:而不仅仅是

编译所有这些C代码,你必须这样做,序列:


- 编译C代码以生成结构

- 运行程序,将struct声明放入文件中

在下一步中使用

- 编译使用新创建的声明的代码


我不清楚为什么你想要一个明确的填充blob。在

我的经历中,大多数那样做的人都试图使

内部C结构布局符合一些外部文件格式 -

通常是一个错误,因为有时很难得到一个内部数据结构来匹配外部约束,并且

通常很容易做出输入和输出操作翻译

介于外部之间和内部格式。翻译代码

可以假设或强制8位常规字节。 (八位字节)使用C

bytes (unsigned char),通常正好是8位,并且

从不少于8位。 外化的八位字节可以完全匹配

到所需的布局。

-

In-Real-Life:克里斯托雷克,风河系统

美国犹他州盐湖城(40°39.22''N,111°50.29''W)+1 801 277 2603

电子邮件:忘了它 http://web.torek.net/torek/index.html

由于垃圾邮件发送者,阅读电子邮件就像在垃圾中搜索食物一样。



The answer is no -- and yes.

There is no way to do it "in one pass", as it were, but you can
still do it by generating the declaration at runtime, then (after
this runtime has finished) use that declaration at a later
compile-time.

This does make some assumptions, such as "a later invocation of
the compiler will use the same layout as an earlier invocation of
the compiler" -- which should be true unless you wait too long (so
that the compiler has been replaced with a later version), or change
the set of flags you pass to the compiler. It also makes the build
process for your software (much) more complicated: instead of just
"compile all this C code", you have to do this, in sequence:

- compile C code to generate the structure
- run the program, putting the struct declaration into a file for
use in the next step
- compile the code that uses the newly-created declaration

It is not clear to me why you want an explicit "padding blob". In
my experience, most people who do that are trying to make the
internal C structure layout meet some external file format -- which
is often a mistake, as it is sometimes quite difficult to get an
internal data structure to match external constraints at all, and
often quite easy to make the input and output operations translate
between "external" and "internal" formats. The translation code
can either assume or force 8-bit "regular bytes" (octets) using "C
bytes" (unsigned char), which are usually exactly 8 bits, and are
never fewer than 8 bits. The "externalized" octets can be matched
to the desired layout completely portably.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


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

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