欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pragma pack、__attribute__的那些事

程序员文章站 2022-05-04 23:34:31
...

在GCC下:struct my{ char ch; int a;}attrubte ((packed)) sizeof(int)=4;sizeof(my)=5

typedef struct mystruct111
{					// 1字节对齐	4字节对齐		2字节对齐
    int a;			// 4			4				4
    char b;			// 1			2(1+1)			2
    short c;		// 2			2				2
	short d;		// 2			4(2+2)			2
}__attribute__((aligned(1024))) My111;

注意__attribute__((aligned(1024))) My111这个整个结构体的对齐,而不是理解成非紧凑模式

百科里写的比较复杂,其实一般用起来也就设置一个对齐长度就行了,即:

#pragma pack(n) //开始对齐,n是对齐长度
 
{
 
此部分定义的数据进行对齐
 
}


```c
#pragma pack () //取消对齐

相关标签: 笔记 c语言