自定义结构体的对齐问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013457167/article/details/82947466
一、跨平台通用数据类型

之前的一篇博客Linux数据类型(通用移植),已经自定义尝试解决了数据通用类型问题。
这里通过源码进行分析,利用源码进行解决问题。在<stdint.h>中我们发现:

typedef signed char int8_t;
typedef unsigned char   uint8_t;
typedef short  int16_t;
typedef unsigned short  uint16_t;
typedef int  int32_t;
typedef unsigned   uint32_t;

通常在跨平台的基于数据结构的网络通信的时候特别要注意字节的对齐方式,我们应该考虑使用定长的数据类型,如uint32_t,4字节的固定长度,并且这属于标准C库(C99),在各系统中都可使用。

	printf("%d\n",sizeof(uint8_t));     //1
	printf("%d\n",sizeof(uint16_t));	//2
	printf("%d\n",sizeof(uint32_t));	//4
二、自定义结构体对齐问题

我们在程序开发过程中往往会遇到这样的问题:以某种数据格式写入,再以此格式读出,特别是socket通信中,通常会遇到数据错位问题,这就是数据结构的对齐的问题。为了让我们的数据结构以最优的方式存储,处理,保证读写数据结构都一一对齐,我们往往采用3种方法:

1) 使用#pragma pack (n)来指定数据结构的对齐值

在缺省情况下,C编译器为每一个变量或是数据单元按其自然对界条件分配空间。一般地,可以通过下面的方法来改变缺省的对界条件:
· 使用伪指令#pragma pack (n),C编译器将按照n个字节对齐。
· 使用伪指令#pragma pack (),取消自定义字节对齐方式。

#pragma pack(push,1)
typedef struct List_t
{
	int a;
	char c;
}List;
#pragma pack(pop)
 
int main()
{
	Student C;
	printf("%ld\n",sizeof(C));
	return 0;
}
输出:
此时按照1字节对齐,因此输出结果为:4+1=5
2) 使用__attribute__ aligned属性指定数据结构的对齐值

__attribute__关键字主要是用来在函数或数据声明中设置其属性。给函数赋给属性的主要目的在于让编译器进行优化。
__attribute((aligned (n)))让所作用的结构成员对齐在n字节自然边界上。如果结构中有成员的长度大于n,则按照最大成员的长度来对齐。

typedef struct Node_t
{
	int a;
	char c;
}__attribute__((aligned(4))) Node;
int main()
{
	Node A;
	printf("%ld\n",sizeof(A));
}
输出:
此时按照4字节对齐,因此输出结果为:4+1+3=8
3) 使用__attribute__ 类型属性packed指定数据结构的对齐值

__attribute__ ((packed)) 取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐。

#include <stdio.h>

typedef struct Student_t
{
	int age;
	char c;
}__attribute__((packed)) Student;
 
 
int main()
{
	Student C;
	printf("%ld\n",sizeof(C));
	return 0;
}
输出:
此时按照实际字节(1字节)对齐,因此输出结果为:4+1=5 
三、代码测试
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <pthread.h>
#include <semaphore.h>
#include <iostream>
//#include <thread>
#include <unistd.h>
#include <stdint.h>

#define START_FLAG 0x7E7E
#define END_FLAG  0xA5A5

typedef enum _OPERATION_CODE{
  SET_CMD_REQ = 0x00,
  QUERY_CMD_REQ = 0x01,
  SET_CMD_RSP = 0x80,
  QUERY_CMD_RSP = 0x81,
  STOP_CTRL_CMD=0xf0,
  FILE_DOWNLOAD_CMD=0xf1,
}OPERATION_CODE;


void display_data(uint8_t *buf ,uint16_t len){
    if(buf){
        int i;
        printf("\n-----buf start------\n");
        for(i=0;i<len;i++){
        
            printf(" %02x",buf[i]);
        }
        printf("\n-----end------\n");
    }else{
        printf("buf is null\n");
    }
}

typedef struct  _PDU_RSP_HEADER{
    uint16_t start_flag;
    uint8_t operation;
    uint8_t code;
    uint16_t len;
    uint32_t data;
    uint16_t crc;
    uint16_t end_flag;
}__attribute__ ((packed)) PDU_CFG_RSP_HEADER_ST;

int main( )

{
	printf("receive data len:%ld\n",sizeof(PDU_CFG_RSP_HEADER_ST));
	PDU_CFG_RSP_HEADER_ST *cfg_hdr = NULL;
	PDU_CFG_RSP_HEADER_ST cfg_cmd_ack={0};
    

     //fill in
	 cfg_cmd_ack.start_flag = START_FLAG;
	 cfg_cmd_ack.operation = SET_CMD_RSP;
     cfg_cmd_ack.code = 0x11;
     cfg_cmd_ack.end_flag = END_FLAG;
     cfg_cmd_ack.len =4;

    uint8_t *buf = reinterpret_cast<uint8_t *>(&cfg_cmd_ack);
	display_data(buf,sizeof(PDU_CFG_RSP_HEADER_ST));

    //fetch out
	cfg_hdr= reinterpret_cast<PDU_CFG_RSP_HEADER_ST *>(buf);
	printf("%x\n",cfg_hdr->start_flag);
	printf("%x\n",cfg_hdr->operation);
	printf("%x\n",cfg_hdr->code);
	printf("%x\n",cfg_hdr->len);
	printf("%x\n",cfg_hdr->end_flag);
	return 0;
}

输出:
receive data len:14

-----buf start------
 7e 7e 80 11 04 00 00 00 00 00 00 00 a5 a5
-----end------
7e7e
80
11
4
a5a5
 

猜你喜欢

转载自blog.csdn.net/u013457167/article/details/82947466