当前位置: 首页 > news >正文

C/C++字节对齐

C/C++字节对齐

  • C/C++字节对齐
    • 1.G_PACKED
    • 2.1 pack(push)
    • 2.2 pack(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))
#endifG_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() // 取消自定义对齐方式

全部例子

#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))
#endifG_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环境
在这里插入图片描述

http://www.lryc.cn/news/246195.html

相关文章:

  • 【Android知识笔记】性能优化专题(四)
  • DC电源模块的散热措施
  • uniapp H5、小程序、APP端自定义不同运行环境(开发、测试、生产)、自定义条件编译平台、以及动态修改manifest.json值讲解
  • centos 显卡驱动安装(chatglm2大模型安装步骤一)
  • 05_属性描述符
  • vue day2
  • 四川劳动保障杂志社四川劳动保障编辑部四川劳动保障杂志2023年第10期目录
  • python数据类型
  • 大数据-之LibrA数据库系统告警处理(ALM-37004 Datanode主备不同步或者断连)
  • 带你用uniapp从零开发一个仿小米商场_2.创建空白项目及公共样式引入
  • 1144. 连接格点,Kruskal算法,二维矩阵压缩为一维
  • C++ : 友元(未完结)
  • Nginx 服务器 SSL 证书安装部署
  • GC9118S低压 5V 全桥驱动芯片,内置过温保护,低电流睡眠模式,可替代TMI8118
  • windows dockerdesktop 安装sqlserver2022
  • 在ubuntu系统安装SVN服务端,并通过客户端进行远程访问
  • STL函数对象-C++
  • Ubuntu 设置Nginx开机自启
  • npm中的npx命令
  • python绘制Z形图 青少年电子学会等级考试 中小学生python编程等级考试一级真题答案解析2023年5月
  • conda环境下module ‘PIL.Image‘ has no attribute ‘ANTIALIAS‘
  • 蓝桥杯每日一题2023.11.26
  • Centos 7.9 Install Docker Insecure Registry
  • 探秘网络通信:UDP与TCP/IP的奥秘
  • Docker的学习笔记
  • 解析直播第三方美颜SDK:技术原理与应用
  • 线程基本方法
  • Linux操作系统 1.初识Linux
  • 分布式事务-两阶段提交2PC
  • 初识Spring (Spring 核心与设计思想)