结构体字节对齐,优点:用小字节的结构体字节对齐方式可以降低储存空间,对于嵌入式等对空间要求比较高的具有很好的优势
缺点:降低了访问速度!
#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;
}
结构体字节对齐是一种节省嵌入式系统存储空间的技术,通过对结构体成员进行字节对齐,可以优化内存布局。尽管这种方式降低了数据访问速度,但在资源有限的嵌入式环境中,它提供了宝贵的存储优化。
5429

被折叠的 条评论
为什么被折叠?



