C语言之——__attribute__
2024-10-07 15:53:12
__attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法。这个功能是跟操作系统没关系,跟编译器有关 。
__attribute__((aligned(4)));设置4字节对齐方式,和#pragma pack(4) 效果一样
可以参考:https://blog.csdn.net/zhangxiong2532/article/details/50826917
#include <stdio.h>
struct mystruct11
{
int a;
char b;
short c;
}__attribute__((packed));
struct mystruct21
{
char a;
int b;
short c;
}__attribute__((packed));
struct p
{
int a;
char b;
char c;
}__attribute__((aligned(4))) p1;
struct q
{
int a;
char b;
struct p qn;
char c;
}__attribute__((aligned(8))) q2;
int main(void)
{
printf("sizeof(struct mystruct11) = %d.\n",sizeof(struct mystruct11));
printf("sizeof(struct mystruct21) = %d.\n",sizeof(struct mystruct21));
/*
两个不同结构体1字节对齐的结构
struct mystruct11 struct mystruct21
1字节对齐 1字节对齐
4 1
1 4
2 2
*/
printf("sizeof(int):%d, sizeof(char)=%d\n", sizeof(int), sizeof(char));
printf("sizeof(p1):%d\n", sizeof(p1));
printf("sizeof(q2):%d\n", sizeof(q2));
return 0;
}
sizeof(struct mystruct11) = 8.
sizeof(struct mystruct21) = 10.
sizeof(int):4, sizeof(char)=1
sizeof(p1):8
sizeof(q2):24
--------------------------------
Process exited after 0.01857 seconds with return value 0
请按任意键继续. . .
最新文章
- CentOS下升级python2.7.10过程记录
- 捉襟见肘之UITableViewCell重用引发的问题
- 补丁vs错误(codevs 2218 错误答案)
- 简单插入排序(C++版)
- 配置《算法 第四版》的Eclipse开发环境
- 不能用100%ie6不兼容
- 这两天dede 仿站的收货
- [图形学] Chp17 OpenGL光照和表面绘制函数
- 兼容IE、火狐、谷歌的页面关闭事件
- Codeforces13E - Holes
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
- Redux的工作流程
- lua 协程的理解
- 一些有用的Java学习资料
- Entity Framework 学习
- 关于Adaboost算法
- oracle学习--循环语句
- KMP算法(——模板习题与总结)
- PHP调用WCF提供的方法
- hadoop笔记 基础 归档