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
2
votes
1 answer

Getting the offset of a member variable via casting a nullptr

I'm looking at the macro offsetof from , and saw that a possible implementation is via #define my_offsetof(type, member) ((void*) &(((type*)nullptr)->member)) I tried it and indeed it works as expected #include #define…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
2
votes
2 answers

Using offsetof() to get owner object from member variable

I would like to implement 'GetParent()' function in here- class ChildClass; class ParentClass { public: .... ChildClass childObj; .... }; class ChildClass { friend class ParentClass; private: ChildClass(); public: …
2
votes
2 answers

Invert pointer to member (i.e. get the address of the containing struct)

I have a struct (Member) that can only be used as data member in some other struct (Container). By convention the name of the member is always m. Is there a reliable way for the member to obtain the address of the containing…
Rumburak
  • 3,416
  • 16
  • 27
2
votes
1 answer

gcc, can I use offsetof() with templated pointer to member?

The code below is here: https://ideone.com/XnxAyw The compiler error I get is: prog.cpp: In member function ‘size_t list_base::offset()’: prog.cpp:26:22: error: expected unqualified-id before ‘*’ token return offsetof(T, *NODE); …
Charlie Skilbeck
  • 1,081
  • 2
  • 15
  • 38
2
votes
1 answer

Printing the offsets of all struct members

My intention is to output a list with offsets of all struct members from a typedef struct. In my case this struct is stored in an external EEPROM which can be accessed bytewise via an interface accessible via a serial connection. To access the…
2
votes
2 answers

offsetof() of nested C struct in C++

I'm trying to add a socket filter to one of my sockets in C++ (Linux). In the socket filter I need to get the offset of struct fork_proc_event, which is nested within another structure. The definition looks like this (cn_proc.h): struct proc_event…
Víctor Fernández
  • 1,754
  • 1
  • 16
  • 15
1
vote
1 answer

Correct usage of offsetof macro

I'm trying to work with the offsetof macro in the following way: typedef unsigned char u8; typedef unsigned short u16; struct MapBlock { u16 type : 10; u8 variant : 3; bool isTop : 1; }; struct MapTile { struct MapBlock top, middle; u16…
Jack
  • 131,802
  • 30
  • 241
  • 343
1
vote
2 answers

is a pointer to data member its offset?

Am I safe to assume that the offset of a data member (offsetof(mystruct, myfield)) is numerically equal to the raw value of a member pointer retrieved with &mystruct::myfield, or is it implementation dependent?
Lorenzo Pistone
  • 5,028
  • 3
  • 34
  • 65
1
vote
1 answer

C++: How to make the compiler optimize memory access in case when a pointer of a member variable is passed elsewhere

[edit: Here is the motivation: passing a pointer of a variable to an external function may accidentally break some optimization for "adjacent" variables, because of the possibility to get pointers to the adjacent variables calculated from the…
zwhconst
  • 1,352
  • 9
  • 19
1
vote
1 answer

How to find space occupied by a struct field and the padding between it and the next field?

So if I have a struct like so: //fwd decl class Payload_t; //msg class for ipc struct Msg { uint16_t Hdr; Payload_t Payload; //whatever type I want here }; I want to get the size of Msg::Hdr including any padding prior to the next field…
1
vote
0 answers

offsetof: seeking clarification of officialese

With reference to offsetof would appreciate a simpler explanation of the following: The expression offsetof(type, member) is never type-dependent and it is value-dependent if and only if type is dependent.
user9196120
  • 381
  • 3
  • 9
1
vote
1 answer

gcc plugin to implement offsetof

I am writing a gcc plugin for parsing the structure fields. But I have the problem how to get the offset of each field in the struct? Just like the offsetof macro in gcc. I see DECL_FIELD_OFFSET in tree.h, but the result seems not right. My code is…
Dotpy
  • 21
  • 3
1
vote
2 answers

C++ Non-Pod starting address

In C, the first element of a struct has the same address as the struct itself. Is the same true for non-POD structs in C++ if the first element is POD? For example, given this code: struct bar { struct bar *p1, *p2; unsigned char…
Marc
  • 32
  • 7
1
vote
1 answer

Will a standard_layout class's data member have a fixed offset from the object's address?

If a class is_standard_layout, is that sufficient to guarantee that a given non-static data member will always have the same offset from the object's address (i.e. same across different instances of that class, process-wide)?
Museful
  • 6,711
  • 5
  • 42
  • 68
1
vote
2 answers

member alignment in c struct-embedded union

I am modifying a bit of C code, that goes roughly like this: typedef struct STRUCT_FOO { ULONG FooInfo; union { ULONG LongData; USHORT ShortData; UCHAR CharData; }; } FOO; ... FOO foo; ULONG dataLength =…
Boris
  • 5,094
  • 4
  • 45
  • 71