__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
请按任意键继续. . .

最新文章

  1. CentOS下升级python2.7.10过程记录
  2. 捉襟见肘之UITableViewCell重用引发的问题
  3. 补丁vs错误(codevs 2218 错误答案)
  4. 简单插入排序(C++版)
  5. 配置《算法 第四版》的Eclipse开发环境
  6. 不能用100%ie6不兼容
  7. 这两天dede 仿站的收货
  8. [图形学] Chp17 OpenGL光照和表面绘制函数
  9. 兼容IE、火狐、谷歌的页面关闭事件
  10. Codeforces13E - Holes
  11. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
  12. Redux的工作流程
  13. lua 协程的理解
  14. 一些有用的Java学习资料
  15. Entity Framework 学习
  16. 关于Adaboost算法
  17. oracle学习--循环语句
  18. KMP算法(——模板习题与总结)
  19. PHP调用WCF提供的方法
  20. hadoop笔记 基础 归档

热门文章

  1. Delphi Treeview 用法(概念、属性、添加编辑插入节点、定位节点、拖拽等)
  2. 英语单词Permissive
  3. JS中数据结构之集合
  4. c#一些操作
  5. MySQL按首字母查询
  6. centos挂载移动硬盘ntfs-3g
  7. 基础复习之HTML (doctype、标签语义化)
  8. mootools使用Request.send()数据时刷新整个页面
  9. rf, xgboost和GBDT对比;xgboost和lightGbm
  10. vue 中 element-ui 引入方式