簡體   English   中英

Python 和 C 結構與默認結構對齊/填充之間的大小不匹配

[英]Size mismatch between Python and C struct with default struct alignment/padding

我有一個結構,我從 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 bytes 與 300 bytes 我已經在 stackoverflowThe Lost Ark of Structure Packing上閱讀過這篇文章,但我無法解釋為什么默認填充/打包規則有這種差異。

無論如何,我通過以這種方式設置結構來解決(之前的 long var 一個地方):

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

struct模塊文檔

填充僅在連續結構成員之間自動添加。 在編碼結構的開頭或結尾處不添加任何填充。

您錯誤地計算了 C 結構的填充 - 假設典型的結構布局和 8 字節長, sizeof(data_E2setupRequest_NodeComponentConfigUpdate_t ) 將是 304 而不是 300,但componentConfigUpdate_s1AP_present實際上進入了您認為在componentConfigUpdate_type之后填充的空間。 額外的 4 個字節的填充實際上位於結構的末尾,而struct不會添加該填充。

同樣來自struct模塊文檔:

要將結構的結尾與特定類型的 alignment 要求對齊,請以該類型的代碼結束格式,重復計數為零。

因此,如果您希望struct將結構的末尾填充為long alignment,您可以在格式字符串的末尾添加0l

(另外, bool是 1 個字節,而不是 4 個 - 在結構中的所有 bool 之后有 3 個字節的填充。您可以通過將所有 bool 彼此相鄰來消除其中的一些填充。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM