Questions tagged [offsetof]

Anything related to the C and C++ `offsetof` macro. `offsetof` is used to determine the offset in bytes of a structure member from the beginning of the structure itself.

Anything related to the C and C++ offsetof macro. offsetof is used to determine the offset in bytes of a structure member from the beginning of the structure itself.

See CPPreference.com documentation for offsetof.

88 questions
59
votes
6 answers

Does &((struct name *)NULL -> b) cause undefined behaviour in C11?

Code sample: struct name { int a, b; }; int main() { &(((struct name *)NULL)->b); } Does this cause undefined behaviour? We could debate whether it "dereferences null", however C11 doesn't define the term "dereference". 6.5.3.2/4 clearly…
M.M
  • 138,810
  • 21
  • 208
  • 365
54
votes
1 answer

Is it possible to get a pointer to one subobject via a pointer to a different, unreleated subobject?

Look at this simple code: struct Point { int x; int y; }; void something(int *); int main() { Point p{1, 2}; something(&p.x); return p.y; } I expect, that main's return value can be optimized to return 2;, as something…
geza
  • 28,403
  • 6
  • 61
  • 135
53
votes
11 answers

Why can't you use offsetof on non-POD structures in C++?

I was researching how to get the memory offset of a member to a class in C++ and came across this on wikipedia: In C++ code, you can not use offsetof to access members of structures or classes that are not Plain Old Data Structures. I tried it out…
Alex
  • 14,973
  • 13
  • 59
  • 94
39
votes
8 answers

Why does this implementation of offsetof() work?

In ANSI C, offsetof is defined as below. #define offsetof(st, m) \ ((size_t) ( (char *)&((st *)(0))->m - (char *)0 )) Why won't this throw a segmentation fault since we are dereferencing a NULL pointer? Or is this some sort of compiler hack…
chappar
  • 7,275
  • 12
  • 44
  • 57
29
votes
1 answer

Do we need to use std::launder when doing pointer arithmetic within a standard-layout object (e.g., with offsetof)?

This question is a follow-up to: Is adding to a "char *" pointer UB, when it doesn't actually point to a char array? In CWG 1314, CWG affirmed that it is legal to perform pointer arithmetic within a standard-layout object using an unsigned char…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
26
votes
1 answer

What does it mean for `offsetof` to be "conditionally-supported" for non standard-layout classes in C++17?

The C++17 Standard says in [support.types.layout]: Use of the offsetof macro with a type other than a standard-layout class is conditionally-supported. And in [defns.cond.supp]: conditionally-supported program construct that an implementation is…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
18
votes
6 answers

Does the 'offsetof' macro from invoke undefined behaviour?

Example from MSVC's implementation: #define offsetof(s,m) \ (size_t)&reinterpret_cast((((s *)0)->m)) // ^^^^^^^^^^^ As can be seen, it dereferences a null pointer, which…
Xeo
  • 129,499
  • 52
  • 291
  • 397
18
votes
1 answer

C++ Compile-Time offsetof inside a template

I have the need to use offsetof from a template with a member selector. I've come up with a way, if you'll excuse the awkward syntax: template constexpr std::size_t offset_of() { …
Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
14
votes
2 answers

Using offsetof to access struct member

I have the following code: #include int main() { struct X { int a; int b; } x = {0, 0}; void *ptr = (char*)&x + offsetof(struct X, b); *(int*)ptr = 42; return 0; } The last line performs indirect access to x.b. Is…
tstanisl
  • 13,520
  • 2
  • 25
  • 40
14
votes
3 answers

Determining struct member byte-offsets at compile-time?

I want to find the byte offset of a struct member at compile-time. For example: struct vertex_t { vec3_t position; vec3_t normal; vec2_t texcoord; } I would want to know that the byte offset to normal is (in this case it should be…
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
13
votes
4 answers

C++ class member variable knowing its own offset

Is it possible to have a member variable, that would be able to calculate pointer to the containing object from pointer to itself (in it's method)? Let's have a foreign call interface wrapped in API like this: template
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
13
votes
4 answers

what is the purpose and return type of the __builtin_offsetof operator?

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?
Dynite
  • 2,313
  • 5
  • 30
  • 38
11
votes
7 answers

Looking for something similar to offsetof() for non-POD types

I'm looking for a way to obtain offsets of data members of a C++ class which is of non-POD nature. Here's why: I'd like to store data in HDF5 format, which seems most suited for my kind of material (numerical simulation output), but it is perhaps a…
yungchin
  • 1,519
  • 2
  • 15
  • 17
10
votes
2 answers

How do one use `offsetof` to access a field in a standard conforming way?

Let's suppose I have a struct and extract the offset to a member: struct A { int x; }; size_t xoff = offsetof(A, x); how can I, given a pointer to struct A extract the member in a standard conforming way? Assuming of course that we have a…
skyking
  • 13,817
  • 1
  • 35
  • 57
9
votes
3 answers

Why is the offsetof macro necessary?

I am new to the C language and just learned about structs and pointers. My question is related to the offsetof macro I recently saw. I know how it works and the logic behind that. In the file the definition is as follows: #define…
user5896153
1
2 3 4 5 6