__builtin_offsetof运算符的用途和返回类型是什么? [英] what is the purpose and return type of the __builtin_offsetof operator?
问题描述
C ++中的__builtin_offsetof运算符(或Symbian中的_FOFF运算符)的用途是什么?
What is the purpose of the __builtin_offsetof operator (or _FOFF operator in Symbian) in C++?
此外,它返回什么?指针?字节数?
In addition what does it return? Pointer? Number of bytes?
推荐答案
这是一个由GCC编译器提供的内置函数,用于实现 offsetof
由C和C ++标准指定的宏:
It's a builtin provided by the GCC compiler to implement the offsetof
macro that is specified by the C and C++ Standard:
它返回POD结构/联合成员的偏移量。
It returns the offset in bytes that a member of a POD struct/union is at.
示例:
struct abc1 { int a, b, c; };
union abc2 { int a, b, c; };
struct abc3 { abc3() { } int a, b, c; }; // non-POD
union abc4 { abc4() { } int a, b, c; }; // non-POD
assert(offsetof(abc1, a) == 0); // always, because there's no padding before a.
assert(offsetof(abc1, b) == 4); // here, on my system
assert(offsetof(abc2, a) == offsetof(abc2, b)); // (members overlap)
assert(offsetof(abc3, c) == 8); // undefined behavior. GCC outputs warnings
assert(offsetof(abc4, a) == 0); // undefined behavior. GCC outputs warnings
@Jonathan提供了一个很好的例子,我记得看过它用来实现入侵的列表(数据项包括next和prev指针本身的列表),但我不记得它在实现它是有帮助的,可悲的是。
@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.
这篇关于__builtin_offsetof运算符的用途和返回类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!