c++ - 无法完全删除结构填充 - 代码块 : Cygwin

标签 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 */
}

最佳答案

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/

相关文章:

malloc 失败会导致死锁或竞争条件吗?

java - 用于搜索字符串的更快的数据结构

algorithm - Big-O 中的时间复杂度

php - 在数据库驱动的网站中使用图形、树和其他高级数据结构的一些示例有哪些?

c++ - 如何清除包含 C 函数声明的字符串中的注释和中间空格?

C++清理控制台窗口

c++ - 在 C++ 中使用 const 表示 std::pair

c++ - 为什么我不断收到错误消息或生成错误消息?

c - 在C程序中使用动态库dll

C 中指针与整数的比较