__builtin_offsetof 运算符的目的和返回类型是什么?
C++ 中 __builtin_offsetof
运算符(或 Symbian 中的 _FOFF
运算符)的用途是什么?
另外它还返回什么? 指针? 字节数?
What is the purpose of __builtin_offsetof
operator (or _FOFF
operator in Symbian) in C++?
In addition what does it return? Pointer? Number of bytes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它是 GCC 编译器提供的内置函数,用于实现 C 和 C++ 标准指定的
offsetof
宏:GCC - offsetof
它返回 POD 结构/联合成员所在的字节偏移量。
示例:
@Jonathan 提供了一个很好的示例,说明您可以在哪里使用它。 我记得曾见过它用于实现侵入式列表(其数据项包括下一个和上一个指针本身的列表),但遗憾的是我不记得它在实现它时有什么帮助。
It's a builtin provided by the GCC compiler to implement the
offsetof
macro that is specified by the C and C++ Standard:GCC - offsetof
It returns the offset in bytes that a member of a POD struct/union is at.
Sample:
@Jonathan provides a nice example of where you can use it. I remember having seen it used to implement intrusive lists (lists whose data items include next and prev pointers itself), but i can't remember where it was helpful in implementing it, sadly.
正如 @litb 指出和 @JesperE 所示,offsetof() 提供以字节为单位的整数偏移量(作为
size_t
值)。你什么时候可以使用它?
一种可能相关的情况是表驱动操作,用于从文件中读取大量不同的配置参数并将这些值填充到同样庞大的数据结构中。 将巨大的内容减少到如此微不足道的程度(并忽略各种必要的现实世界实践,例如在标头中定义结构类型),我的意思是某些参数可以是整数和其他字符串,并且代码可能看起来隐约像
:现在编写一个通用函数,从配置文件中读取行,丢弃注释和空行。 然后,它隔离参数名称,并在
desc_configuration
表中查找(您可以对其进行排序,以便可以进行二分搜索 - 多个 SO 问题都解决了这个问题)。 当它找到正确的 config_desc 记录时,它可以将找到的值和 config_desc 条目传递给两个例程之一 - 一个用于处理字符串,另一个用于处理整数。这些函数的关键部分是:
这避免了必须为结构的每个单独成员编写单独的函数。
As @litb points out and @JesperE shows, offsetof() provides an integer offset in bytes (as a
size_t
value).When might you use it?
One case where it might be relevant is a table-driven operation for reading an enormous number of diverse configuration parameters from a file and stuffing the values into an equally enormous data structure. Reducing enormous down to SO trivial (and ignoring a wide variety of necessary real-world practices, such as defining structure types in headers), I mean that some parameters could be integers and others strings, and the code might look faintly like:
You can now write a general function that reads lines from the config file, discarding comments and blank lines. It then isolates the parameter name, and looks that up in the
desc_configuration
table (which you might sort so that you can do a binary search - multiple SO questions address that). When it finds the correctconfig_desc
record, it can pass the value it found and theconfig_desc
entry to one of two routines - one for processing strings, the other for processing integers.The key part of those functions is:
This avoids having to write a separate function for each separate member of the structure.
内置
__offsetof
运算符的目的是编译器供应商可以继续#defineoffsetof()
宏,但让它与定义一元的类一起使用>运算符&
。offsetof()
的典型 C 宏定义仅在(&lvalue)
返回该右值的地址时才起作用。 IEThe purpose of a built-in
__offsetof
operator is that the compiler vendor can continue to #define anoffsetof()
macro, yet have it work with classes that define unaryoperator&
. The typical C macro definition ofoffsetof()
only worked when(&lvalue)
returned the address of that rvalue. I.e.正如@litb所说:结构/类成员的字节偏移量。 在 C++ 中,有时它是未定义的,以防编译器抱怨。 IIRC,实现它的一种方法(至少在C中)是这样做
但我确信这存在问题,但我将把它留给感兴趣的读者指出......
As @litb, said: the offset in bytes of a struct/class member. In C++ there are cases where it is undefined, in case the compiler will complain. IIRC, one way to implement it (in C, at least) is to do
But I'm sure there are problems this, but I'll leave that to the interested reader to point out...