1. 程式人生 > >自定義結構體的對齊問題

自定義結構體的對齊問題

一、跨平臺通用資料型別

之前的一篇部落格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