谈谈 C/C++ 中的 offsetof

转载自:https://liam0205.me/2018/01/10/a-customized-offsetof-in-Cpp/

介绍

offsetof 是源自 C 语言的宏,它接受两个参数(类型名和成员名),返回一个 std::size_t 类型的常量表达式。offsetof 的返回值是成员在该类型对象中以字节计算的的偏移量。其中,传入计算的类型名,必须满足标准内存布局的要求;即

  • 所有非 static 数据成员的访问控制权限相同;
  • 没有虚函数;
  • 不从虚基类继承;
  • 所有非 static 数据成员都不是引用类型;
  • 所有非 static 数据成员类型和基类都满足上述要求。

若传入计算的类型名不满足内存布局的要求,或者求解的成员是 static 成员或成员函数,则调用该宏是未定义行为(Undefined Behaviour)。

实现

按照定义,有

  1. offsetof(s, m) 的值只与类型和成员有关,也就是说,在计算 offsetof(s, m) 的时候,不应传入 s 类型具体某个对象,也不应为计算该值而临时构造一个对象;
  2. offsetof(s, m) 的值,其单位是字节;
  3. offsetof(s, m) 的值应是 std::size_t 类型。

offsetof 的这三个特性,也是实现 offsetof 宏的三个难点。为了解决这些问题,首先,实现应当让编译器相信在某处存在一个「虚拟的」但是「可用的」对象。而后,根据该虚拟对象,可以取得目标成员 m 的地址。随后,利用 m 的地址与该虚拟对象的起始地址做差,即可得知 m 的偏移量;为了求得以字节为单位的 ptrdiff_t,需将 m 的地址转变为 char 类型的指针。最后,只需将 ptrdiff_t 转换为 std::size_t 即可。

因此,有如下 C++ 实现:

#define offsetof(s, m) (size_t)((char*)(&((s*)0)->m))

此处,通过 static_cast<s*>(nullptr),编译器相信在 nullptr 处(0x0)有一个真实存在的 s 类型的对象。此处使用 static_cast 而非 reinterpret_cast 是因为 C++ 标准不允许将 nullptr 通过 reinterpret_cast 转换成其他类型的指针;此类转换应用 static_cast。由于 static_cast<s*>(nullptr) 返回指向 s 类型对象的指针,因此 static_cast<s*>(nullptr)->m 就是一个虚拟但在编译器看来可用的成员变量 m。为了求得以字节为单位的 ptrdiff_t,实现中通过 &reinterpret_cast<const volatile char&>(static_cast<s*>(nullptr)->m) 获得一个 const volatile char* 类型的变量。由于在该实现中,虚拟的变量位于 0x0 位置,故而 &reinterpret_cast<const volatile char&>(static_cast<s*>(nullptr)->m) 即是 m 在 s 类型对象当中相对对象起始地址的偏移量。最后,只需将它转换为 size_t 类型的值即可:reinterpret_cast<size_t>(&reinterpret_cast<const volatile char&>(static_cast<s*>(nullptr)->m))


同样,可以有 C 风格的实现:

#define offsetof(s, m) ((size_t)&((*s)0)->m)

测试

#include <stdio.h>
#define offsetof(s, m) (reinterpret_cast<size_t>(&reinterpret_cast<const volatile char&>(static_cast<s*>(nullptr)->m)))

struct S {
    char c;
    double d;
    char cc;
};

int main(void) {
    printf("the first element is at offset %zu\n", offsetof(struct S, c));
    printf("the double is at offset %zu\n", offsetof(struct S, d));
    printf("the third element is at offset %zu\n", offsetof(struct S, cc));
}


上述测试代码的结果是:

$ ./a.out
the first element is at offset 0
the double is at offset 8
the third element is at offset 16



  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 或 C++ ,计算一个对象所占用的空间大小相对比较简单。 对于 C 语言,我们可以使用 `sizeof` 运算符来计算一个对象的大小,例如: ```c #include <stdio.h> #include <stdlib.h> int main() { int a; printf("Size of int: %ld bytes\n", sizeof(a)); return 0; } ``` 上述代码将输出 `Size of int: 4 bytes`,表示 `int` 类型所占用的空间大小为 4 个字节。 对于 C++ 语言,我们同样可以使用 `sizeof` 运算符来计算一个对象的大小,例如: ```c++ #include <iostream> using namespace std; int main() { int a; cout << "Size of int: " << sizeof(a) << " bytes" << endl; return 0; } ``` 上述代码将输出 `Size of int: 4 bytes`,表示 `int` 类型所占用的空间大小为 4 个字节。 需要注意的是,对于结构体和类等复杂类型的对象,其大小取决于其成员变量的类型和数量,需要逐个计算并累加,可以使用 `sizeof` 运算符和 `offsetof` 宏来计算。例如: ```c++ #include <iostream> #include <cstddef> using namespace std; struct Person { string name; int age; }; int main() { Person p; cout << "Size of Person: " << sizeof(p) << " bytes" << endl; cout << "Offset of name: " << offsetof(Person, name) << " bytes" << endl; cout << "Offset of age: " << offsetof(Person, age) << " bytes" << endl; return 0; } ``` 上述代码将输出 `Size of Person: 16 bytes`,表示 `Person` 类型所占用的空间大小为 16 个字节,其 `name` 字符串占用 8 个字节,`age` 整数占用 4 个字节,剩余 4 个字节为内存对齐所需的填充字节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值