【发布时间】:2018-03-11 15:48:05
【问题描述】:
结构“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
如何将大小从 516 减小到 512?
编辑 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 */
}
【问题讨论】:
-
重新排列结构以便不需要填充?如果不知道结构 是 以及它包含什么,或者结构应该用于什么,那么真的很难为您提供帮助。
-
一个答案——我意识到这可能不是你正在寻找的答案——如果你试图删除填充的原因是你可以得到准确的结构匹配一些外部强加的布局(可能是文件格式或网络协议),从长远来看,一次解压一个字段的数据通常更容易和更便携。尝试正确定义结构,以便您可以使用
fread一次阅读所有内容并神奇地正确,这很有吸引力,但很难做到正确,正如您所发现的那样。 (然后是字节顺序...) -
如果您想得到这个问题的答案,而不是猜测或推测,您需要将完整的结构声明添加到您的问题中。
-
你在这个结构中有指针吗?你用的是整数吗?根据架构,指针和整数可以有不同的大小。如需帮助,请提供源结构和目标结构。
-
@SteveSummit 是的,我正在尝试将结构与库规定的布局相匹配。代码处理输入文件的结构大小必须正好为 512 字节。
标签: c++ c data-structures