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:

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 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.

Answer from Johannes Schaub - litb on Stack Overflow

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:

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 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.

Answer from Johannes Schaub - litb on Stack Overflow
Top answer
1 of 4
17

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:

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 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.

2 of 4
15

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:

#include <stddef.h>

typedef stuct config_info config_info;
struct config_info
{
   int parameter1;
   int parameter2;
   int parameter3;
   char *string1;
   char *string2;
   char *string3;
   int parameter4;
} main_configuration;

typedef struct config_desc config_desc;
static const struct config_desc
{
   char *name;
   enum paramtype { PT_INT, PT_STR } type;
   size_t offset;
   int   min_val;
   int   max_val;
   int   max_len;
} desc_configuration[] =
{
    { "GIZMOTRON_RATING", PT_INT, offsetof(config_info, parameter1), 0, 100, 0 },
    { "NECROSIS_FACTOR",  PT_INT, offsetof(config_info, parameter2), -20, +20, 0 },
    { "GILLYWEED_LEAVES", PT_INT, offsetof(config_info, parameter3), 1, 3, 0 },
    { "INFLATION_FACTOR", PT_INT, offsetof(config_info, parameter4), 1000, 10000, 0 },
    { "EXTRA_CONFIG",     PT_STR, offsetof(config_info, string1), 0, 0, 64 },
    { "USER_NAME",        PT_STR, offsetof(config_info, string2), 0, 0, 16 },
    { "GIZMOTRON_LABEL",  PT_STR, offsetof(config_info, string3), 0, 0, 32 },
};

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 correct config_desc record, it can pass the value it found and the config_desc entry to one of two routines - one for processing strings, the other for processing integers.

The key part of those functions is:

static int validate_set_int_config(const config_desc *desc, char *value)
{
    int *data = (int *)((char *)&main_configuration + desc->offset);
    ...
    *data = atoi(value);
    ...
}

static int validate_set_str_config(const config_desc *desc, char *value)
{
    char **data = (char **)((char *)&main_configuration + desc->offset);
    ...
    *data = strdup(value);
    ...
}

This avoids having to write a separate function for each separate member of the structure.

🌐
Wikipedia
en.wikipedia.org › wiki › Offsetof
offsetof - Wikipedia
October 29, 2025 - This builtin is especially useful with C++ classes that declare a custom unary ... It is useful when implementing generic data structures in C. For example, the Linux kernel uses ... #define CONTAINER_OF(ptr, Type, member) ({ \ const typeof(((Type*)0)->member) *__mptr = (ptr); \ (Type*)((char*)__mptr - offsetof(Type, member));})
Discussions

failed build: defining a type within '__builtin_offsetof' is a Clang extension
[root@sbc-stage-a0 envoy]# bazel build -c opt --config=clang --verbose_failures envoy INFO: Analyzed target //:envoy (814 packages loaded, 38567 targets configured). INFO: Found 1 target... ERROR: ... More on github.com
🌐 github.com
5
April 26, 2023
Getting weird errors with the usage of offsetof() function
You are not using the offsetof macro correctly. The usage is offsetof (type, member), not offsetof (type, type::member) — it is not a member pointer or anything like that that you're passing but the name of the member. More generally, you can't call something incorrectly and expect it to work just because one implementation happens to accept it and do the right thing. More on reddit.com
🌐 r/cpp_questions
24
1
August 12, 2018
MSVC C++ compliance: Using __builtin_offsetof
Hi folks, This is in regard to the definition of PROTOBUF_FIELD_OFFSET: protobuf/src/google/protobuf/port_def.inc Lines 311 to 314 in 0644388 #define PROTOBUF_FIELD_OFFSET(TYPE, FIELD) \ static_cas... More on github.com
🌐 github.com
5
February 4, 2025
Problems in __builtin_offsetof locations - Clang Frontend - LLVM Discussion Forums
The attached C source show two different problems: $ llvm_new/Debug/bin/clang-cc -W -Wall bug_clang2.c bug_clang2.c:11:7: error: too many arguments to function call fun(333); ~~~ ^~~ bug_clang2.c:12:7: error: to… More on discourse.llvm.org
🌐 discourse.llvm.org
0
November 4, 2009
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Offsetof.html
Offsetof (Using the GNU Compiler Collection (GCC))
GCC implements for both C and C++ a syntactic extension to implement the offsetof macro. primary: "__builtin_offsetof" "(" typename "," offsetof_member_designator ")" offsetof_member_designator: identifier | offsetof_member_designator "." identifier | offsetof_member_designator "[" expr "]"
🌐
GitHub
gist.github.com › graphitemaster › 494f21190bb2c63c5516
Working around offsetof limitations in C++ · GitHub
Various people would have you believe it's not undefined since it tends to be the common technique implored to implement the offsetof macro. Modern compilers do provide intrinsics however; (GCC, Clang) now use __builtin_offsetof because they're now beginning to optimize based on the assumption code does not depend on undefined behavior.
🌐
GitHub
github.com › gcc-mirror › gcc › blob › master › gcc › testsuite › c-c++-common › builtin-offsetof.c
gcc/gcc/testsuite/c-c++-common/builtin-offsetof.c at master · gcc-mirror/gcc
__builtin_offsetof(struct B, p[11]); // { dg-warning "greater than size" } __builtin_offsetof(struct B, a.p); // OK · __builtin_offsetof(struct B, p[0]); // OK ·
Author   gcc-mirror
Find elsewhere
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › stddef.h › offsetof
C Programming/stddef.h/offsetof - Wikibooks, open books for an open world
#define offsetof(st, m) __builtin_offsetof(st, m) [edit | edit source] It is useful when implementing generic data structures in C.
🌐
GitHub
github.com › envoyproxy › envoy › issues › 26973
failed build: defining a type within '__builtin_offsetof' is a Clang extension · Issue #26973 · envoyproxy/envoy
April 26, 2023 - failed build: defining a type within '__builtin_offsetof' is a Clang extension#26973 · Copy link · Labels ·
Author   sergey-safarov
🌐
Reddit
reddit.com › r/cpp_questions › getting weird errors with the usage of offsetof() function
r/cpp_questions on Reddit: Getting weird errors with the usage of offsetof() function
August 12, 2018 -

At some point in my code (while mapping the OpenGL buffer) I have used offsetof() to specify the byte offset values:

glVertexAttribPointer(SHADER_VERTEX_LOCATION, 3, GL_FLOAT, GL_FALSE, VERTEX_SIZE, (const GLvoid *) (offsetof(VertexData, VertexData::vertex))));

The above code runs fine through GCC on both Linux and Windows, but it shows errors with Clang on Mac.

The errors I am getting are such:

expected ')'
...VERTEX_SIZE, (const GLvoid *) (offsetof(VertexData, VertexData::vertex))));
                                                                   ^
to match this '('
...3, GL_FLOAT, GL_FALSE, VERTEX_SIZE, (const GLvoid *) (offsetof(VertexData, VertexData::vertex))));
                                                         ^
expanded from macro 'offsetof'
#define offsetof(t, d) __builtin_offsetof(t, d)
                                          ^

I found out that offsetof() has compiler dependent implementations, which could mean that Clang is messing something up.

EDIT:

Okay, I am adding an example here:

#include <iostream>
#include <cstdef>

struct VertexData
{
    int a;
    int b;
    int c;
};

int main()
{
    std::cout << offsetof(VertexData, VertexData::a) << std::endl;
}

This is working inside Visual Studio, but not in VS Code.

I guess this might be because of how offsetof() has been defined in cstddef.

I found something like this:

#if defined _MSC_VER && !defined _CRT_USE_BUILTIN_OFFSETOF
    #ifdef __cplusplus
        /* This line is active in Visual Studio on Windows */ 
        // So far this definition is working correctly with the same sort of use
        #define offsetof(s,m) ((::size_t)&reinterpret_cast<char const volatile&>((((s*)0)->m)))
    #else
        #define offsetof(s,m) ((size_t)&(((s*)0)->m))
    #endif
#else
    /* My Mac system uses this definition, which is currently throwing a compilation error */
    #define offsetof(s,m) __builtin_offsetof(s,m)
#endif
🌐
GitHub
github.com › protocolbuffers › protobuf › issues › 20229
MSVC C++ compliance: Using __builtin_offsetof · Issue #20229 · protocolbuffers/protobuf
February 4, 2025 - Fortunately, MSVC supports __builtin_offsetof, so we ask that you use that instead.
Author   Radnyx
🌐
Narkive
gcc-help.gcc.gnu.narkive.com › RSR7ee4n › issues-with-buildtin-offsetof-and-workarounds
Issues with __buildtin_offsetof() and workarounds
Many of these assertions have a non-const expression in the member argument of offsetof. It would be a shame to get rid of this safety net. The proprietary compiler that we use still defines offsetof as #define _FOFF(c,f) (((TInt)&(((c *)0x1000)->f))-0x1000) and works. GCC falls over in many instances, thus we switched to __builtin_offsetof().
🌐
LLVM
reviews.llvm.org › D133574
⚙ D133574 [C2x] reject type definitions in offsetof
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm made very clear that it is an UB having type definitions with in offsetof. After this patch clang will reject any type definitions in __builtin_offsetof · Fixes https://github.com/llvm/llvm-project/issues/57065
🌐
LLVM Discussion Forums
discourse.llvm.org › clang frontend
Problems in __builtin_offsetof locations - Clang Frontend - LLVM Discussion Forums
November 4, 2009 - The attached C source show two different problems: $ llvm_new/Debug/bin/clang-cc -W -Wall bug_clang2.c bug_clang2.c:11:7: error: too many arguments to function call fun(333); ~~~ ^~~ bug_clang2.c:12:7: error: too many arguments to function call fun(__builtin_offsetof(struct s, g)); ~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bug_clang2.c:13:7: error: too many arguments to function call fun(__builtin_offsetof(struct s, f)); ~~~ ^ 3 diagnostics generated.
🌐
Developer Community
developercommunity.visualstudio.com › content › problem › 296176 › -builtin-offsetof-causing-c1903-error-and-no-other.html
__builtin_offsetof causing C1903 error and no other ...
July 24, 2018 - Skip to main content · Microsoft · Visual Studio · Sign in · You need to enable JavaScript to run this app · Sorry this browser is no longer supported · Please use any other modern browser like 'Microsoft Edge'
🌐
Wg21
wg21.link › P0908r0
p0908r0: Offsetof for Pointers to Members - WG21 Links
February 11, 2018 - offsetof with pointer-to-member member-designators should simply work. Modern compilers implement offsetof using an extension (__builtin_offsetof in GCC and LLVM), so implementation need not require library changes.
🌐
Google Groups
groups.google.com › g › llvm-dev › c › l78RQ9zJR64
[LLVMdev] Evaluation of offsetof() macro
On Fri, Jan 02, 2015 at 05:26:42PM -0800, Vadim Chugunov wrote: > My goal is to be able to use the result in constexpr contexts (like > declaring an array, whose size expression involves offsetof()). Please > correct me if this is wrong, but I think this means that the expression > must be evaluated before any IR is emitted. Do you start from offsetof() or the expanded form? For various reasons, it is normally defined to use __builtin_offsetof, since the problems you have run into also stop the "legacy" C version from being appropoiate for C++ as it doesn't create an ICE.
🌐
Open-std
open-std.org › jtc1 › sc22 › wg21 › docs › papers › 2018 › p0908r0.html
p0908r0: Offsetof for Pointers to Members
February 11, 2018 - offsetof with pointer-to-member member-designators should simply work. Modern compilers implement offsetof using an extension (__builtin_offsetof in GCC and LLVM), so implementation need not require library changes.