Offsetof


C's offsetof macro is an ANSI C library feature found in stddef.h. It evaluates to the offset of a given member within a struct or union type, an expression of type size_t. The offsetof macro takes two parameters, the first being a structure name, and the second being the name of a member within the structure. It cannot be described as a C prototype.

Implementation

The "traditional" implementation of the macro relied on the compiler obtaining the offset of a member by specifying a hypothetical structure that begins at address zero:

  1. define offsetof \
&->m))

This can be understood as taking a null pointer of type structure st, and then obtaining the address of member m within said structure. While this implementation works correctly in many compilers, it has generated some debate if this is undefined behavior according to the C standard, since it appears to involve a dereference of a null pointer. It also tends to produce confusing compiler diagnostics if one of the arguments is misspelled.
An alternative is:

  1. define offsetof \
&->m - )

It may be specified this way because the standard does not specify that the internal representation of the null pointer is at address zero. Therefore the difference between the member address and the base address needs to be made. Again, since these are constant expressions it can be calculated at compile time and not necessarily at run-time.
Some modern compilers define the macro using a special form instead, e.g.

  1. define offsetof \
__builtin_offsetof

This builtin is especially useful with C++ classes or structs that declare a custom unary operator &.

Usage

It is useful when implementing generic data structures in C. For example, the Linux kernel uses offsetof to implement container_of, which allows something like a mixin type to find the structure that contains it:

  1. define container_of

This macro is used to retrieve an enclosing structure from a pointer to a nested element, such as this iteration of a linked list of my_struct objects:

struct my_struct ;
extern struct list_node * list_next;
struct list_node *current = /*... */
while

The linux kernel implementation of container_of uses a GNU C extension called statement expressions. It's possible a statement expression was used to ensure type safety and therefore eliminate potential accidental bugs. There is, however, a way to implement the same behaviour without using statement expressions while still ensuring type safety:

  1. define container_of : &->member) - offsetof))

At first glance, this implementation may seem more complex than necessary, and the unusual use of the conditional operator may seem out of place. A simpler implementation is possible:

  1. define container_of - offsetof))

This implementation would also serve the same purpose, however, there's a fundamental omission in terms of the original linux kernel implementation. The type of ptr is never checked against the type of the member, this is something that the linux kernel implementation would catch.
In the aforementioned type-checked implementation, the check is performed by the unusual use of the conditional operator. The constraints of the conditional operator specify that if the operands to the conditional operator are both pointers to a type, they must both be pointers to compatible types. In this case, despite the fact that the value of the third operand of the conditional expression will never be used, the compiler must perform a check to ensure that and &->member are both compatible pointer types.

Limitations

Usage of offsetof is limited to POD types in C++98, standard-layout classes in C++11, and more cases are conditionally-supported in C++17, otherwise it has an undefined behavior. While most compilers will generate a correct result even in cases that don't respect the standard, there are edge cases when offsetof will either yield an incorrect value, generate a compile-time warning or error, or outright crash the program. This is especially the case for virtual inheritance.
The following program will generate several warnings and print obviously suspicious results when compiled with gcc 4.7.3 on an amd64 architecture:

  1. include
  2. include
struct A
struct B: public virtual A
int main

Output is:

offsetof : 8
offsetof : 8