C/C++字节对齐(pack)

本文详细介绍了在C++中使用G_PACKED宏和pragmapack进行字节对齐,展示了如何在GCC和Windows/Linux环境下实现结构体的1字节对齐,并提供了编译时的条件编译示例以及结构体大小的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.C/C++字节对齐

1.G_PACKED

#ifdef __GNUC__
  #define G_PACKED( __Declaration__ ) __Declaration__ __attribute__((packed))
#else
  #define G_PACKED( __Declaration__ ) __pragma( pack(push,1)) __Declaration__ __pragma( pack(pop))
#endif

G_PACKED(
typedef struct __abc
{
    uint8_t  Forced_lock; /*< 数据,赋值操作,*/
    uint32_t  reserve; /*< 数据,赋值操作,预留*/
    uint8_t  hover_now; /*< 数据,赋值操作,*/
}) abc_s;

2.1 pack(push)

#pragma pack(push) //保存对齐状态
#pragma pack(1)//设定为1字节对齐
typedef struct
{
    uint8_t msgType;	//消息类型
    uint16_t exitReason;	//退出原因
} fileTransmitExit_t;
#pragma pack(pop)//恢复对齐状态

2.2 pack(1)

#pragma pack(1) // 按照1个字节对齐
typedef struct
{
    uint8_t msgType;	//消息类型
    uint16_t exitReason;	//退出原因
}fileTransmitExit_t4;
#pragma pack() // 取消自定义对齐方式

2.全部例子

#pragma once
#include <iostream>

using namespace std;

#ifdef __GNUC__
  #define G_PACKED( __Declaration__ ) __Declaration__ __attribute__((packed))
#else
  #define G_PACKED( __Declaration__ ) __pragma( pack(push,1)) __Declaration__ __pragma( pack(pop))
#endif

G_PACKED(
typedef struct __abc
{
    uint8_t  Forced_lock; /*< 数据,赋值操作,*/
    uint16_t  reserve; /*< 数据,赋值操作,预留*/
    uint8_t  hover_now; /*< 数据,赋值操作,*/
}) test1;

#pragma pack(push) //保存对齐状态
#pragma pack(1)//设定为1字节对齐
typedef struct
{
    uint8_t msgType;	//消息类型
    uint16_t exitReason;	//退出原因
    uint8_t msgType2;
} test2;
#pragma pack(pop)//恢复对齐状态

#pragma pack(1)//设定为1字节对齐
typedef struct
{
    uint8_t msgType;	//消息类型
    uint16_t exitReason;	//退出原因
    uint8_t msgType2;
}test3;
#pragma pack()//恢复对齐状态

typedef struct
{
    uint8_t msgType;	//消息类型
    uint16_t exitReason;	//退出原因
    uint8_t msgType2;
}__attribute__ ((aligned(1))) test4;

typedef struct
{
    uint8_t msgType;	//消息类型
    uint16_t exitReason;	//退出原因
    uint8_t msgType2;
}__attribute__((packed)) test5;

int main()
{
    cout << "Hello World!" << endl;
    printf("test1 size1:%d\n", sizeof (test1));
    printf("test2 size2:%d\n", sizeof (test2));
    printf("test3 size3:%d\n", sizeof (test3));
    printf("test4 size4:%d\n", sizeof (test4));
    printf("test5 size4:%d\n", sizeof (test5));
    return 0;
}

Qt windows 输出结果
在这里插入图片描述
ubuntu2004 linux环境
在这里插入图片描述

3.windows和linux兼容对齐

24.5.18 增加

//开始
#ifdef __GNUC__
    //linux
    #ifdef __GNUC__
      #define G_PACKED( __Declaration__ ) __Declaration__ __attribute__((packed))
    //#else
    //#define G_PACKED( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )
    #endif
#elif WIN32
    #pragma pack(push) //保存对齐状态
    #pragma pack(1)//设定为1字节对齐
#endif
//结束
#ifdef WIN32
    #pragma pack(pop)//恢复对齐状态
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静思心远

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值