Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和具有默认结构对齐/填充的C结构之间的大小不匹配_Python_C_Python 3.x_Data Structures_Struct - Fatal编程技术网

Python和具有默认结构对齐/填充的C结构之间的大小不匹配

Python和具有默认结构对齐/填充的C结构之间的大小不匹配,python,c,python-3.x,data-structures,struct,Python,C,Python 3.x,Data Structures,Struct,我有一个从C服务器发送到Python客户端的结构。C结构如下所示: // Data structure to be sent to python interface typedef struct { // uint32_t componentType; // 4 bool componentId_present;

我有一个从C服务器发送到Python客户端的结构。C结构如下所示:

// Data structure to be sent to python interface
typedef struct {                                        //
    uint32_t componentType;                             // 4
    bool componentId_present;                           // 1 + 3 padding = 4
    uint32_t componentIdType;                           // 4 + 4 padding = 8
    long componentId;                                   // 8
    uint32_t componentConfigUpdate_type;                // 4 + 4 padding = 8
    bool componentConfigUpdate_s1AP_present;            // 1 + 3 padding = 4
    uint32_t componentConfigUpdate_s1AP_size;           // 4
    byte componentConfigUpdate_s1AP[128];               // 128
    bool componentConfigUpdate_x2AP_present;            // 1 + 3 padding = 4
    uint32_t componentConfigUpdate_x2AP_size;           // 4
    byte componentConfigUpdate_x2AP[128];               // 128
} data_E2setupRequest_NodeComponentConfigUpdate_t;      // 256 + 3*8 + 6*4 = 256 + 24 + 24 = 304
在Python中,我使用以下代码计算要接收的大小:

import struct
size = struct.calcsize("i?ili?i128s?i128s")             # returns 300
正如您所看到的,大小是不同的:304字节与300字节。我读过和,但我无法解释为什么默认填充/打包规则会有这样的差异

无论如何,我通过这样设置结构解决了这个问题(之前有一个地方):

从:

填充仅在连续结构成员之间自动添加。未在编码结构的开头或结尾添加填充

您计算错了C结构的填充-假设典型的结构布局和8字节的长度,
sizeof(data\u E2setupRequest\u nodecomponent configupdate\u t
)将是304而不是300,但是
componentConfigUpdate\u s1AP\u present
实际上进入了您认为在
componentConfigUpdate\u type
之后填充的空间。额外的4字节填充实际上位于结构的末尾,而
struct
不会添加该填充

也可从
struct
模块文档:

要将结构的结尾与特定类型的对齐要求对齐,请使用该类型的代码结束格式,重复计数为零

因此,如果希望
struct
将结构的末尾填充到
long
对齐中,可以将
0l
添加到格式字符串的末尾


(另外,
bool
是1个字节,而不是4个字节-在结构中的所有bool之后有3个字节的填充。您可以通过将所有bool相邻放置来消除部分填充。)

在哪个体系结构上?(32/64位,endianness…)我是从一个小小的endianx86_64 GNU/Linux编译的
typedef struct {
    uint32_t componentType;                             // 4
    bool componentId_present;                           // 1 + 3 padding = 4
    long componentId;                                   // 8
    uint32_t componentIdType;                           // 4 + 0 padding = 4
    uint32_t componentConfigUpdate_type;                // 4 + 0 padding = 4
    bool componentConfigUpdate_s1AP_present;            // 1 + 3 padding = 4
    ....
} data_E2setupRequest_NodeComponentConfigUpdate_t;      // 256 + 8 + 8*4 = 256 + 8 + 32 = 296
import struct
size = struct.calcsize("i?lii?i128s?i128s")             # returns 296