#pragma pack(n)和__attribute__((aligned(m)))的区别
2016-01-20 09:37
281 查看
#pragma pack(n)和__attribute__((aligned(m)))的区别:
前 者告诉编译器结构体或类内部的成员变量相对于第一个变量的地址的偏移量的对齐方式,缺省情况下,编译器按照自然边界对齐,当变量所需的自然对齐边界比n大 时,按照n对齐,否则按照自然边界对齐;后者告诉编译器一个结构体或者类或者联合或者一个类型的变量(对象)分配地址空间时的地址对齐方式。也就是所,如 果将__attribute__((aligned(m)))作用于一个类型,那么该类型的变量在分配地址空间时,其存放的地址一定按照m字节对齐(m必
须是2的幂次方)。并且其占用的空间,即大小,也是m的整数倍,以保证在申请连续存储空间的时候,每一个元素的地址也是按照m字节对齐。 __attribute__((aligned(m)))也可以作用于一个单独的变量。举例说明:
#include<stdio.h>
#pragma pack(4)
typedef struct{
uint32_t f1;
uint8_t f2;
uint8_t f3;
uint32_t f4;
uint64_t f5;
}__attribute__((aligned(1024))) ts;
int main()
{
printf("Struct size is: %d, aligned on 1024\n",sizeof(ts));
printf("Allocate f1 on address: 0x%x\n",&(((ts*)0)->f1));
printf("Allocate f2 on address: 0x%x\n",&(((ts*)0)->f2));
printf("Allocate f3 on address: 0x%x\n",&(((ts*)0)->f3));
printf("Allocate f4 on address: 0x%x\n",&(((ts*)0)->f4));
printf("Allocate f5 on address: 0x%x\n",&(((ts*)0)->f5));
return 0;
}
输出:
Struct size is: 1024, aligned on 1024
Allocate f1 on address: 0x0
Allocate f2 on address: 0x4
Allocate f3 on address: 0x5
Allocate f4 on address: 0x8
Allocate f5 on address: 0xc
注意
绿色部分表明了__attribute__((aligned(1024))) 的作用
红色部分说明#pragma pack(4)只对大小大于4的成员变量的地址偏移起作用
紫色部分说明对于大小大于4的成员变量,其地址偏移按照4字节对齐
前 者告诉编译器结构体或类内部的成员变量相对于第一个变量的地址的偏移量的对齐方式,缺省情况下,编译器按照自然边界对齐,当变量所需的自然对齐边界比n大 时,按照n对齐,否则按照自然边界对齐;后者告诉编译器一个结构体或者类或者联合或者一个类型的变量(对象)分配地址空间时的地址对齐方式。也就是所,如 果将__attribute__((aligned(m)))作用于一个类型,那么该类型的变量在分配地址空间时,其存放的地址一定按照m字节对齐(m必
须是2的幂次方)。并且其占用的空间,即大小,也是m的整数倍,以保证在申请连续存储空间的时候,每一个元素的地址也是按照m字节对齐。 __attribute__((aligned(m)))也可以作用于一个单独的变量。举例说明:
#include<stdio.h>
#pragma pack(4)
typedef struct{
uint32_t f1;
uint8_t f2;
uint8_t f3;
uint32_t f4;
uint64_t f5;
}__attribute__((aligned(1024))) ts;
int main()
{
printf("Struct size is: %d, aligned on 1024\n",sizeof(ts));
printf("Allocate f1 on address: 0x%x\n",&(((ts*)0)->f1));
printf("Allocate f2 on address: 0x%x\n",&(((ts*)0)->f2));
printf("Allocate f3 on address: 0x%x\n",&(((ts*)0)->f3));
printf("Allocate f4 on address: 0x%x\n",&(((ts*)0)->f4));
printf("Allocate f5 on address: 0x%x\n",&(((ts*)0)->f5));
return 0;
}
输出:
Struct size is: 1024, aligned on 1024
Allocate f1 on address: 0x0
Allocate f2 on address: 0x4
Allocate f3 on address: 0x5
Allocate f4 on address: 0x8
Allocate f5 on address: 0xc
注意
绿色部分表明了__attribute__((aligned(1024))) 的作用
红色部分说明#pragma pack(4)只对大小大于4的成员变量的地址偏移起作用
紫色部分说明对于大小大于4的成员变量,其地址偏移按照4字节对齐
相关文章推荐
- Android利用webView调用JavaScript
- 缓存机制--笔记
- 实践干货!猿题库 iOS 客户端架构设计
- 响应式初认识
- linux常用工具集合
- Linux---CentOS下编译安装Apache后只显示测试页面解决方法
- MVC Html.AntiForgeryToken() 防止CSRF攻击
- SQLite数据库用法
- kali linux 2.0下安装metasploit
- linux(CentOS)安装phpstorm
- (转)C#正则表达式Regex类的用法
- 如何在2016年成为一个更好的 Node.js 开发者
- 【更新】Kendo UI Professional Q1 2016 Beta全新发布
- PDP 上下文 (2011-03-31 17:40:27) http://blog.sina.com.cn/s/blog_798a531d0100qe9q.html
- Android06_Android资源访问
- typedef中的typedef void sigfunc(int);
- 那些GitHub上很有名气的大牛们
- Java不同压缩算法的性能比较
- vim 颜色主题设置
- 看VR小黄片究竟是种怎样的体验?(有视频噢)