结构体内存偏移
来源:互联网 发布:哪家证券公司好些 知乎 编辑:程序博客网 时间:2024/04/26 07:54
#define OffSet(type, field) &((type*)0)->field
struct MyStr
{
char a;
short b;
int c;
float d;
double e;
char f;
};
printf("%d\n", OffSet(MyStr, a));
printf("%d\n", OffSet(MyStr, b));
printf("%d\n", OffSet(MyStr, c));
printf("%d\n", OffSet(MyStr, d));
printf("%d\n", OffSet(MyStr, e));
printf("%d\n", OffSet(MyStr, f));
0
2
4
8
16
24
linux的struct list_head利用了这个。
传统的方法实现链表是在对象结构体内使用指针prev,next,造成的问题是链表的操作需要自己来实现。
改进如下:
struct list_head { struct list_head *next, *prev;};
在需要使用链表的对象结构体内添加一个struct list_head域即可形成链表,linux提供了对链表进行各种操作的宏,基本原理同上,不再赘述。感觉C语言太神奇了,博大精深啊。
0 0
- 结构体内存偏移
- 求结构体内存偏移地址
- 结构体内存对齐
- 结构体内存对齐
- 结构体内存对齐
- 结构体内存布局
- 结构体内存对齐
- 结构体内存对齐
- 结构体内存对齐
- 结构体内存对齐
- 结构体内存对齐
- 结构体内存对齐
- 结构体内存对齐
- 结构体内存对齐
- 结构体内存分配
- 结构体内存释放
- 结构体内存对齐
- 结构体内存对齐
- 模拟新浪微博随便看看栏目
- POJ1692:Crossed Matchings
- 图结构练习——最小生成树
- 爱情让人智商变低
- 在windows2012 R2下安装oracle 11.2.0.4的GI时遇到的问题--bug导致
- 结构体内存偏移
- 一步一步跟我学习lucene(6)---lucene索引优化之多线程创建索引
- 利用cygwin编译cholmod以获得在windows上可用的库lib
- ubuntu查看端口命令
- 编译安装Xen 4.5(适用于CentOS6 Redhat6)
- 前后台系统
- 急救常识读书笔记-4
- 第一题
- 深入剖析Android消息机制