ccastingpragma-pack

Is `#pragma pack(1)` risky/dangerous if a struct contains only byte arrays


Is it dangerous/risky to use #pragma pack(1) on structs that contain byte-arrays only? E.g. this one:

#pragma pack(1)
struct RpcMessage {
    uint8_t proto_info[16];
    uint8_t message_uuid[16];

    uint8_t arg0[16];
    uint8_t arg1[16];
    uint8_t arg2[16];
    uint8_t arg3[16];
    uint8_t arg4[16];
    uint8_t arg5[16];
    uint8_t arg6[16];
    uint8_t arg7[16];

    uint8_t payload[65376];
};

(The idea is to cast this struct directly to/from 2^16 raw I/O bytes without any incompatibilities or even faults)


Solution

  • If the structure only contains uint8_t, then #pragma pack(1) will have no effect at all. It simply won’t do anything, because the structure is already packed as tightly as it can be.

    Padding will only appear if you have elements which have larger than byte alignment.