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

C语言之——__attribute__的更多相关文章

  1. C语言中__attribute__ ((at())绝对定位的应用

    C语言中的关键字__attribute__ ,当时大一学C语言中没有接触过,后来工作中搞RFID的蓝牙标签卡开发,用的是MSP430G2332,直接用的是绝对定位: 1 const uint8_t f ...

  2. C语言之预处理

    这是2016年的最后一篇博客,年初定的计划是写12篇博客,每月一篇,1/3转载,2/3原创,看来是实现不了了! -- 题外话.今天要写的东西是C语言中的预处理器,我们常说的宏定义的用法.为什么要写这个 ...

  3. u-boot源码分析之C语言段

    题外话: 最近一直在学习u-boot的源代码,从代码量到代码风格,都让我认识到什么才是真正的程序.以往我所学到的C语言知识和u-boot的源代码相比,实在不值一提.说到底,机器都是0和1控制的.感觉这 ...

  4. C语言结构体对齐

    1.结构体变量中的元素如何访问? (1)数组中元素的访问方式:表面上有2种方式(数组下标方式和指针方式):实质上都是指针方式访问.(2)结构体变量中的元素访问方式:只有一种,用.或者->的方式来 ...

  5. [汇编与C语言关系]4. 结构体和联合体

    用反汇编的方法研究一下C语言的结构体: #include <stdio.h> int main(int argc, char ** argv) { struct { char a; sho ...

  6. C语言中的强符号与弱符号

    转自:http://blog.csdn.net/astrotycoon/article/details/8008629 一.概述 在C语言中,函数和初始化的全局变量(包括显示初始化为0)是强符号,未初 ...

  7. iOS宏和__attribute__

    本文目录 iOS宏的经典用法 Apple的习惯 __attribute__ iOS宏的经典用法 1.常量宏.表达式宏 #define kTabBarH (49.0f) #define kScreenH ...

  8. __attribute__

    转来的: http://www.cnblogs.com/astwish/p/3460618.html __attribute__ 你知多少? GNU C 的一大特色就是__attribute__ 机制 ...

  9. __attribute__((packed))详解

      1. __attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法.这个功能是跟操作系统没关系,跟编译器有关 ...

随机推荐

  1. 07.整合jsp、整合freemarker、整合thymeleaf

    整合jsp pom.xml部分内容 <packaging>war</packaging> </dependencies> <dependency> &l ...

  2. js 中HTML的 onkeycode 和onkeydown属性事件

    <!DOCTYPE html><html><head><script>function displayResult(){var x=document.g ...

  3. 内存中的Buffer和Cache的区别

    Reference:https://time.geekbang.org/column/article/74633 磁盘是一个块设备,可以划分为不同的分区:在分区之上再创建文件系统,挂载到某个目录,之后 ...

  4. C# webbrowser专题

    C# .Net 2.0实例学习:WebBrowser页面与WinForm交互技巧 2 Study Case :高亮显示 上一个例子中我们学会了查找文本——究跟到底,对Web页面还是只读不写.那么,如果 ...

  5. soj#552 449E Jzzhu and Squares

    分析 https://www.cnblogs.com/Memory-of-winter/p/11209128.html 代码 #include<bits/stdc++.h> using n ...

  6. 术语-Portal:Portal(Web站点)

    ylbtech-术语-Portal:Portal(Web站点) Portal作为网关服务于因特网的一种WEB站点.Portal是链路.内容和为用户可能找到的感兴趣的信息(如新闻.天气.娱乐.商业站点. ...

  7. C#-Newtonsoft.Json生成复杂JSON

    官方文档:https://www.newtonsoft.com/json/help/html/SerializeObject.htm 一种方式就可以生成所有的 JSON Collection -> ...

  8. selenium2-java 浏览器cookie的获取

    //成功登陆后增加如下代码        File cookieFile = new File("C:\\tmp\\tangdai.cookie.txt");           ...

  9. 关系型数据库MySQL(四)_备份与还原

    数据库备份 备份命令:mysqldump 备份一个数据库 mysqldump -h localhost -u username -p password database_name > D:\fi ...

  10. Bootstrap 学习笔记3 路径分页标签和徽章组件