无法完全删除结构填充 - Codeblocks:Cygwin

时间:2018-03-11 15:48:06

标签: c++ c data-structures

结构'item_header'的大小,根据元素的大小应该是512字节,计算为520字节(使用sizeof)。

enter image description here

我试图删除填充,但只能将大小降低到516字节。 enter image description here

我发现删除填充的两个步骤是:

#pragma pack(push,1)         :-  This step reduced the size by 4 bytes
__attribute__ ((packed))     :-  This step had no effect on the structure size

如何将尺寸缩小到512而不是516?

编辑1:结构声明:

struct item_header {
    char    history[256];   /* processing history */
    char    params[128];    /* special processing parameters */
    int32   processdate;    /* processing date */
    int32   datatype;       /* data type: speech, lx, etc */
    int32   subtype;        /* data sub-type: natural, synthetic, etc */
    int32   floating;       /* fixed or floating data */
    int32   datasize;       /* data item size (bytes) */
    int32   framesize;      /* no. items in time frame */
    int32   numframes;      /* no. frames in data */
    int32   length;         /* overall length (bytes) */
    char    comment[20];    /* data set comment */
    int32   windowsize;     /* size of analysis window in samples */
    int32   overlap;        /* size of analysis overlap in samples */
    int32   lxsync;         /* flag :larynx syncronous=1,fixed frame=0 */
    int32   lastposn;       /* last frame position */
    char    spare[40];      /* space for expansion */
    int32   machine;        /* machine code: 0=68000, 1=8086 */
    int32   datapresent;    /* data present 1=yes,0=no */
    double  frameduration;  /* time interval duration (s) */
    double  offset;         /* cumulative time offset */
}

1 个答案:

答案 0 :(得分:3)

256字节:

char    history[256];

128字节:

char    params[128];

8 * 4字节= 32字节:

int32   processdate;
int32   datatype;
int32   subtype;
int32   floating;
int32   datasize;
int32   framesize;
int32   numframes;
int32   length;

20个字节:

char    comment[20];

4 * 4字节= 16字节:

int32   windowsize;
int32   overlap;
int32   lxsync;
int32   lastposn;

40个字节:

char    spare[40];

2 * 4字节= 8字节:

int32   machine;
int32   datapresent;

2 * 8字节= 16字节:

double  frameduration;
double  offset;

256 + 128 + 32 + 20 + 16 + 40 + 8 + 16 = 总共516个字节

你有太多的字段或者某些东西比它需要的大(或者你依赖的文档是错误的,或者......)。