根据元素的大小,结构“item_header”的大小应该为 512 字节,计算结果为 520 字节(使用 sizeof)。
我发现删除填充的两个步骤是:
#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 */
}
最佳答案
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 字节。
您的字段太多或某些字段大于所需的字段(或者您所依赖的文档有误,或者...)。
关于c++ - 无法完全删除结构填充 - 代码块 : Cygwin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49221883/