首页 > 其他 > 详细

offsetof

时间:2017-05-12 14:12:33      阅读:359      评论:0      收藏:0      [点我收藏+]

offsetof,程序语言,该宏用于求结构体中一个成员在该结构体中的偏移量

struct student{int id ,int age,char * address}; id 占4字节,相对于结构体偏移0字节,age占4字节,相对于结构体偏移4字节,address相对于结构体偏移8字节。

若struct student{char sex ,int age,char * address};sex为1字节,相对于结构体偏移0字节,age 占4字节,相对于结构体偏移4字节. address偏移8字节。

因为有字节对齐机制。sex后面的3字节都是空闲的。这样做是为了提高计算机的读取效率。

offsetof本身就是偏移的意思。就是查看偏移量的。

offsetof(struct student,id)  0  offsetof(struct student,age) 4  offsetof(struct student,address) 8

offsetof(struct student,sex)  0 offsetof(struct student,age) 4  offsetof(struct student,address) 8

offsetof

原文:http://www.cnblogs.com/coversky/p/6845330.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!