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
4
votes
5 answers

About offsetof usage in C and C++?

In C, I saw some usage of offsetof to calculate the offset of a member in the structure to its beginning? Is it still recommended to use in C++? Any other way to do this without this macro?
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
4
votes
4 answers

Standard way to find base address of struct from a member

struct Data { int a; std::string b; float c; }; std::string* allocateDataAndGetString() { Data* dataPtr(someAllocator.allocate()); return &dataPtr.b; } Data* getBaseDataPtrFromString(std::string* mStringMember) { //…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
3
votes
2 answers

Passing the offset of a field as a template parameter to that field

What I am trying to do is to have a class which is aware of its offset within an enclosing class with no runtime overhead at all. Here's an example of what I wish I could do: template struct Inner { }; struct Outer { int…
antonin_scalia
  • 1,073
  • 2
  • 10
  • 11
3
votes
0 answers

Accessing a member variable through the parent class and an offset

I put the tag language lawyer, although I have the feeling that this is on the wrong side of the standard boundary. I haven't seen a conversation exactly on this point, and but I had at work, so I would like to have some certainty about this. The…
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
3
votes
2 answers

Using offsetof for template classes

From the C++ standard: A standard-layout class is a class that: — has no non-static data members of type non-standard-layout class (or array of such types) or reference, — has no virtual functions (10.3) and no virtual base classes (10.1), — has…
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
3
votes
5 answers

Why does a non-constant offsetof expression work?

Why does this work: #include #include #include typedef struct x { int a; int b[128]; } x_t; int function(int i) { size_t a; a = offsetof(x_t, b[i]); return a; } int main(int argc, char…
Chris J. Kiick
  • 1,487
  • 11
  • 14
2
votes
3 answers

Getting every offsetParent or the total offSetTop and total offSetLeft

I want to get the total offSetTop and the total offSetLeft of a child element which have many level of parent element and may be adding up. Is that any shorthand way, besides of adding one by one in manual ways?
Kent Liau
  • 915
  • 15
  • 26
2
votes
0 answers

C++ Use pointer-to-member and absolute address of member to get address of object

With a class definition, an object pointer and a pointer-to-member, it is possible to obtain an absolute pointer to that member: class X { public: int a { 0 }; int b { 1 }; int c { 2 }; }; X x; X* xptr = &x; int X::* pointer_to_member =…
Kai Petzke
  • 2,150
  • 21
  • 29
2
votes
2 answers

Is it legal C to obtain the pointer to a struct from the pointer to its 2nd member?

I'm wondering if the line preceded by the comment "Is this legal C?" (in the function dumpverts() at the bottom) is legal C or not: #include #include #include struct stvertex { double x; double y; …
cesss
  • 852
  • 1
  • 6
  • 15
2
votes
1 answer

Is possible to detect that a C struct has a field?

I need to detect by a macro at compile time if a struct has a member. I've tried offsetof(struct object, a_field) but it causes error to be issued at compilation about non existing field. Is there some other method to check if a C struct has a…
psprint
  • 349
  • 1
  • 10
2
votes
2 answers

What is correct way to access a struct using pointer and offsetof()

I have the following code so as to be able to access numerous fields in array of structs (I've reduced it to two for simplicity). What is the correct incantation for the final pointer calculation *(ptr + offset) = data; because I always get…
Mike Bryant
  • 310
  • 2
  • 12
2
votes
2 answers

Does this implementation of offsetof invoke undefined behavior?

offsetof is defined like this in stddef.h: #define offsetof(type, member) ((size_t)&((type *)0)->member) Does this invoke undefined behavior due to the dereference of a NULL pointer? If not, why?
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
2
votes
2 answers

g++ won't let me pass a template parameter to offsetof

When using g++ I pass a template parameter as the member variable to offsetof, and I get the following warning: invalid access to non-static data member 'SomeClass::t' of NULL object (perhaps the 'offsetof' macro was used incorrectly) Here is what…
impulsionaudio
  • 219
  • 2
  • 7
2
votes
3 answers

c++ reinterpret_cast a integer

I came across following c++ code: #define OFFSETOF_MEMBER(t, f) \ (reinterpret_cast(&reinterpret_cast(16)->f) - static_cast(16u)) // NOLINT where t is a type, f is a field name. I wonder why can we put a integer 16 as…
Chao Wang
  • 21
  • 2
2
votes
0 answers

Using offsetof() to get pointer to member for member-agnostic functions

Suppose I have a struct like: typedef struct S S; struct S { S *next; // ... S *gc_next; }; I.e., it contains multiple "next" pointers to be simultaneously on multiple linked lists. If I want to write a single function that can follow…
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88