C语言之——__attribute__
__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__的更多相关文章
- C语言中__attribute__ ((at())绝对定位的应用
C语言中的关键字__attribute__ ,当时大一学C语言中没有接触过,后来工作中搞RFID的蓝牙标签卡开发,用的是MSP430G2332,直接用的是绝对定位: 1 const uint8_t f ...
- C语言之预处理
这是2016年的最后一篇博客,年初定的计划是写12篇博客,每月一篇,1/3转载,2/3原创,看来是实现不了了! -- 题外话.今天要写的东西是C语言中的预处理器,我们常说的宏定义的用法.为什么要写这个 ...
- u-boot源码分析之C语言段
题外话: 最近一直在学习u-boot的源代码,从代码量到代码风格,都让我认识到什么才是真正的程序.以往我所学到的C语言知识和u-boot的源代码相比,实在不值一提.说到底,机器都是0和1控制的.感觉这 ...
- C语言结构体对齐
1.结构体变量中的元素如何访问? (1)数组中元素的访问方式:表面上有2种方式(数组下标方式和指针方式):实质上都是指针方式访问.(2)结构体变量中的元素访问方式:只有一种,用.或者->的方式来 ...
- [汇编与C语言关系]4. 结构体和联合体
用反汇编的方法研究一下C语言的结构体: #include <stdio.h> int main(int argc, char ** argv) { struct { char a; sho ...
- C语言中的强符号与弱符号
转自:http://blog.csdn.net/astrotycoon/article/details/8008629 一.概述 在C语言中,函数和初始化的全局变量(包括显示初始化为0)是强符号,未初 ...
- iOS宏和__attribute__
本文目录 iOS宏的经典用法 Apple的习惯 __attribute__ iOS宏的经典用法 1.常量宏.表达式宏 #define kTabBarH (49.0f) #define kScreenH ...
- __attribute__
转来的: http://www.cnblogs.com/astwish/p/3460618.html __attribute__ 你知多少? GNU C 的一大特色就是__attribute__ 机制 ...
- __attribute__((packed))详解
1. __attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法.这个功能是跟操作系统没关系,跟编译器有关 ...
随机推荐
- Linux用户登出之后保持后台进程(nohup)
使用&可以将进程置于后台,但是用户从Shell登出之后,进程会自动结束.想要在登出之后保持进程运行,就要结合nohup命令使用. 例如: nohup find -size +100k > ...
- 【leetcode】1020. Partition Array Into Three Parts With Equal Sum
题目如下: Given an array A of integers, return true if and only if we can partition the array into three ...
- mybatis框架之装饰模式
学习开源框架源码,除了储备点知识以便于与面试官互相忽略之外,我想最重要的还是去学习大神如何写代码,如何做到职责单一,如何做到可扩展等... 本篇,试着总结一下mybatis在缓存模块使用到的装饰模式. ...
- wangeditor 粘贴word内容带样式
这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用 后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下) ...
- flutter中的列表组件
列表布局是我们项目开发中最常用的一种布局方式.Flutter 中我们可以通过 ListView 来定义列表项,支持垂直和水平方向展示.通过一个属性就可以控制列表的显示方向.列表有以下分类: 垂直列表 ...
- ibatis 的使用
1. 文本的使用 select ‘day’+Num from Table;//Sql select convert(varchar,'day')+Num from Table;//ibatis
- asp label居中
https://stackoverflow.com/questions/25245839/align-a-label-to-the-right-using-asp-net Try something ...
- jmeter添加自定义扩展函数之小写转换大写
1,打开eclipse,新建maven工程,在pom中引用jmeter核心jar包,具体请看---https://www.cnblogs.com/guanyf/p/10863033.html---,这 ...
- python Condition类(锁)
Condition(条件变量)通常与一个锁关联.需要在多个Contidion中共享一个锁时,可以传递一个Lock/RLock实例给构造方法,否则它将自己生成一个RLock实例. 不理解锁的,请看上一条 ...
- 基于全局地址池的DHCP
一.实验目的 二.实验拓扑图 .三.实验编址 四.实验步骤 1.配置IP 2.配置基于全局地址池的DHCP server 使用IP pool命令创建一个新的全局地址池,名称为hjt1 配置hjt1可动 ...