sizeof显示结构的不同大小 [英] sizeof shows different size for a structure

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

问题描述

大家好,


我是C新手,所以请保持温柔。我有一个程序定义

以下内容:


typedef union

{

unsigned int I;

unsigned char b [4];

} dword;


/ * DAWG记录0的结构。 * /

typedef struct

{

unsigned char magic [5];

unsigned char name [11] ;

unsigned char title [41];

unsigned char desc [41];

unsigned char author [41];

unsigned char extra [41];

unsigned char ch [64];

unsigned char numchars;

unsigned char catsym [ 8];

unsigned char catname [8] [11];

unsigned char catinclude [8] [8];

unsigned char numcategories ;

dword numnodes;

} dawghdr;


我希望dawghdr结构的大小为410字节。但是,在linux上使用gcc 3.3.2编译这个程序时,
使用了412

字节。我已经检查过这个结构的内存空间了,看起来好像是
在numcategories和numnodes之间出现了2个额外的字节

成员。有没有人能够解释为什么这可能会发生?b / b
该程序将此结构作为记录写入磁盘文件,我就是这样的/>
后来用python脚本读取了这个输出文件,但是脚本因为无法读取numnodes的正确值而导致
结束,因为它有

无法知道2个额外字节。

Hi everyone,

I''m a C newbie, so please be gentle. I have a program that defines the
following things:

typedef union
{
unsigned int I;
unsigned char b[4];
} dword;

/* Structure for record 0 of the DAWG. */
typedef struct
{
unsigned char magic[5];
unsigned char name[11];
unsigned char title[41];
unsigned char desc[41];
unsigned char author[41];
unsigned char extra[41];
unsigned char ch[64];
unsigned char numchars;
unsigned char catsym[8];
unsigned char catname[8][11];
unsigned char catinclude[8][8];
unsigned char numcategories;
dword numnodes;
} dawghdr;

I expect the size of the dawghdr structure to be 410 bytes. However,
on compiling this program with gcc 3.3.2 on linux, dawghdr uses 412
bytes. I''ve examined the memory space of this structure, and it seems
to me that 2 extra bytes appear between the numcategories and numnodes
members. Could anyone be kind enough to explain why this might be
happening?

The program writes this structure to a disk file as a record, and I
later read this output file with a python script, but the script falls
over, as it fails to read the correct value of numnodes, since it has
no way of knowing about the 2 extra bytes.

推荐答案

Anand Buddhdev< ar*@anand.org>写道:
Anand Buddhdev <ar*@anand.org> wrote:
我希望dawghdr结构的大小为410字节。但是,在linux上使用gcc 3.3.2编译这个程序时,dawghdr使用了412个字节。我已经检查了这个结构的内存空间,似乎对我来说,numcategories和numnodes
成员之间会出现2个额外的字节。任何人都可以善意解释为什么会发生这种情况?


允许编译器在元素之间添加填充。

这个问题包含在常见问题中:
http://www.eskimo.com/~scs/C-faq/q2 .13.html

程序将此结构作为记录写入磁盘文件,稍后我用python脚本读取此输出文件,但脚本属于
over,因为它无法读取numnodes的正确值,因为它无法知道2个额外的字节。
I expect the size of the dawghdr structure to be 410 bytes. However,
on compiling this program with gcc 3.3.2 on linux, dawghdr uses 412
bytes. I''ve examined the memory space of this structure, and it seems
to me that 2 extra bytes appear between the numcategories and numnodes
members. Could anyone be kind enough to explain why this might be
happening?
The compiler is allowed to put padding between the elements.
This question is coverd in the faq:
http://www.eskimo.com/~scs/C-faq/q2.13.html
The program writes this structure to a disk file as a record, and I
later read this output file with a python script, but the script falls
over, as it fails to read the correct value of numnodes, since it has
no way of knowing about the 2 extra bytes.




这个"溶液"高度依赖于编译器。你应该写一个自己的

方法。并且不要忘记处理结束。


Ciao Chriss



This "solution" is highly compiler dependent. You should write an own
method. And don''t forget to take care of endianess.

Ciao Chriss




" ; Anand Buddhdev < ar*@anand.org>在消息中写道

news:7b ************************** @ posting.google.c om ...

"Anand Buddhdev" <ar*@anand.org> wrote in message
news:7b**************************@posting.google.c om...
大家好,

我是C新手,所以请保持温柔。我有一个程序来定义以下内容:

typedef union
{unsigned int I;
unsigned char b [4];
} dword;

/ * DAWG记录0的结构。 * /
typedef struct
{
unsigned char magic [5];
unsigned char name [11];
unsigned char title [41];
unsigned char desc [41];
unsigned char author [41];
unsigned char extra [41];
unsigned char ch [64];
unsigned char numchars;
unsigned char catsym [8];
unsigned char catname [8] [11];
unsigned char catinclude [8] [8];
unsigned char numcategories;
dword numnodes;
} dawghdr;

我希望dawghdr结构的大小为410字节。但是,在linux上使用gcc 3.3.2编译这个程序时,dawghdr使用了412个字节。我已经检查了这个结构的内存空间,似乎对我来说,numcategories和numnodes
成员之间会出现2个额外的字节。任何人都可以善意解释为什么会发生这种情况?


Conpiler padding。检查编译器的文档

程序将此结构作为记录写入磁盘文件,然后我用python脚本读取此输出文件,但脚本属于
over,因为它无法读取numnodes的正确值,因为它无法知道2个额外的字节。
Hi everyone,

I''m a C newbie, so please be gentle. I have a program that defines the
following things:

typedef union
{
unsigned int I;
unsigned char b[4];
} dword;

/* Structure for record 0 of the DAWG. */
typedef struct
{
unsigned char magic[5];
unsigned char name[11];
unsigned char title[41];
unsigned char desc[41];
unsigned char author[41];
unsigned char extra[41];
unsigned char ch[64];
unsigned char numchars;
unsigned char catsym[8];
unsigned char catname[8][11];
unsigned char catinclude[8][8];
unsigned char numcategories;
dword numnodes;
} dawghdr;

I expect the size of the dawghdr structure to be 410 bytes. However,
on compiling this program with gcc 3.3.2 on linux, dawghdr uses 412
bytes. I''ve examined the memory space of this structure, and it seems
to me that 2 extra bytes appear between the numcategories and numnodes
members. Could anyone be kind enough to explain why this might be
happening?
Conpiler padding. Check your compiler''s documentation
The program writes this structure to a disk file as a record, and I
later read this output file with a python script, but the script falls
over, as it fails to read the correct value of numnodes, since it has
no way of knowing about the 2 extra bytes.




尝试写(和阅读)结构元素。使用这种方式

你不在乎填充。



Try writing(and reading) the structure element by element. Using that way
you don''t care about padding.


Anand Buddhdev写道:


....
Anand Buddhdev wrote:

....
我希望dawghdr结构的大小为410字节。但是,在linux上使用gcc 3.3.2编译这个程序时,dawghdr使用了412个字节。
I expect the size of the dawghdr structure to be 410 bytes. However,
on compiling this program with gcc 3.3.2 on linux, dawghdr uses 412
bytes.



....

如果大声喊叫,请不要*任何人*在发布前查看常见问题解答?


....

For crying out loud, doesn''t *anyone* check the FAQ before posting?


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

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