结构体字节对齐,优点:用小字节的结构体字节对齐方式可以降低储存空间,对于嵌入式等对空间要求比较高的具有很好的优势
缺点:降低了访问速度!
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <string>
using namespace std;
struct A_1
{
int a;
unsigned __int64 b;
short c;
};
#
struct B_1
{
int a;
short c;
unsigned __int64 b;
};
#pragma pack(1)
struct A
{
int a;
unsigned __int64 b;
short c;
};
struct B
{
int a;
short c;
unsigned __int64 b;
};
int main()
{
cout<<"a 结构体对齐之前:"<<sizeof(A_1)<<endl;
cout<<"b 结构体对齐之前:"<<sizeof(B_1)<<endl;
cout<<"a 之后:"<<sizeof(A)<<endl;
cout<<"b 之后:"<<sizeof(B)<<endl;
}