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.

Filter by
Sorted by
Tagged with
4 votes
1 answer
117 views

does offsetof work with typeof if type is new?

I read on cppreference (https://en.cppreference.com/w/c/types/offsetof): Even though it is specified in C23 that defining a new type in offsetof is undefined behavior, such usage is only partially ...
Abdulmalek Almkainzi's user avatar
2 votes
0 answers
57 views

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

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 }; }; ...
Kai Petzke's user avatar
  • 2,239
0 votes
1 answer
222 views

How to calculate reconciliation factor in CRC32 for CVN number and CALID

How to calculate reconciliation factor in CRC32 for CVN number and CALID Can someone help he with this How to calculate reconciliation factor in CRC32 and crc16. but calculated factor value should be ...
Mehul's user avatar
  • 3
0 votes
0 answers
171 views

Is there an elegant way to access protected members with gcc typeof & offsetof from outside the class?

I'm working on a project written in both C and C++. The C part has a macro used to get a pointer to an object from a pointer to one of its members #define START_OF_OBJ(ptr, type, member)({\ const ...
Uri Raz's user avatar
  • 435
2 votes
2 answers
168 views

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 <stdio.h> #include <stdlib.h> #...
cesss's user avatar
  • 862
0 votes
0 answers
91 views

How did developers code before container_of macros were invented?

I'm aware of what container_of macro does, but I'm far from understanding it. I know one can get a struct reference as long as having its member's name, member's type, and member's reference. For ...
Mr.nerd3345678's user avatar
14 votes
2 answers
1k views

Using offsetof to access struct member

I have the following code: #include <stddef.h> int main() { struct X { int a; int b; } x = {0, 0}; void *ptr = (char*)&x + offsetof(struct X, b); *(int*)ptr = 42; ...
tstanisl's user avatar
  • 13.8k
0 votes
0 answers
72 views

PostgreSQL source code - offsetof() + sizeof() to get size of a stuct?

I was scanning the source code of pgSQL and found an interesting macro. #define SizeOfXLogRecord (offsetof(XLogRecord, xl_crc) + sizeof(pg_crc32c)) The definition of the XLogRecord is typedef ...
lumos's user avatar
  • 1
0 votes
1 answer
134 views

Boolean expression evaluator on struct members

Background I have a struct: struct event { uint16_t id; uint8_t type; std::string name; // many more fields (either string or integer types) }; A boolean expression(stored in a string)...
psy's user avatar
  • 914
2 votes
1 answer
386 views

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 ...
psprint's user avatar
  • 349
0 votes
1 answer
76 views

is the pointerdiff between members of different objects in containers the same?

The c++ macro offsetof is just defined behaviour when used on standard layout types. As I understood this is because the compiler can change the memory layout of the data depending on the context of ...
user3520616's user avatar
0 votes
0 answers
101 views

Finding struct in array of structs from member value in C

I am trying to make a function, that lets me look in an array of structs, and return the one whose value of a certain member matches the input. I found another thread, where a guy had a familiar issue,...
victorsoeby's user avatar
1 vote
1 answer
211 views

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 ...
zwhconst's user avatar
  • 1,372
0 votes
1 answer
350 views

Is this use of offsetof guaranteed to be correct?

I have read that offsetof macro is commonly implemented as: #define offsetof(st, m) \ ((size_t)&(((st *)0)->m)) And according to Wikipedia there is debate about whether this is undefined ...
Zebrafish's user avatar
  • 12.1k
0 votes
1 answer
416 views

Copying part of a struct using memcpy and offsetof

I'd like to copy part of a struct starting from a certain element forward by combining the offsetof macro and memcpy, as shown below: #include <stdio.h> #include <string.h> #include <...
Gian's user avatar
  • 327
0 votes
3 answers
134 views

Safe way to reference nested member

I have a struct with some other structs as member. Both external and internal structs are StandardLayout (it can be even assumed that internal are plain old data). Something like this: struct Inner1 {...
mihaild's user avatar
  • 280
3 votes
2 answers
505 views

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<int offset&...
antonin_scalia's user avatar
2 votes
2 answers
1k views

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 ...
Mike Bryant's user avatar
54 votes
1 answer
2k views

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 ...
geza's user avatar
  • 28.7k
0 votes
0 answers
169 views

Using <stddef.h> in prog_kern.c ebpf

I've seen that instead of using #include <stddef.h> to have access to offsetof macro, ebpf programs conditionally define the macro inside the prog_kern.c file. Is it wrong to include stddef.h in ...
Maicake's user avatar
  • 1,056
2 votes
2 answers
571 views

Does this implementation of offsetof invoke undefined behavior? [duplicate]

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, ...
S.S. Anne's user avatar
  • 15.3k
29 votes
1 answer
919 views

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 ...
Brian Bi's user avatar
  • 113k
3 votes
0 answers
327 views

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 ...
Matthieu Brucher's user avatar
1 vote
1 answer
91 views

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::...
neener neener's user avatar
4 votes
1 answer
178 views

Does offsetof require pointer derefence?

I am wondering whether a simple macro offset_of_ requires a pointer dereference of not. For example, a C++ (means that this code will be compiled using a C++ compiler) struct which is declared with ...
Ta Thanh Dinh's user avatar
2 votes
3 answers
2k views

c++ reinterpret_cast a integer

I came across following c++ code: #define OFFSETOF_MEMBER(t, f) \ (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u)) // NOLINT ...
Chao Wang's user avatar
1 vote
0 answers
73 views

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 ...
user9196120's user avatar
1 vote
1 answer
218 views

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 ...
Dotpy's user avatar
  • 21
27 votes
1 answer
3k views

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-...
Vittorio Romeo's user avatar
5 votes
1 answer
538 views

Why offsetof implementations strangely differs on C and C++?

I opened stddef.h and saw this: #if defined _MSC_VER && !defined _CRT_USE_BUILTIN_OFFSETOF #ifdef __cplusplus #define offsetof(s,m) ((size_t)&reinterpret_cast<char const ...
Artem Selivanov's user avatar
4 votes
1 answer
141 views

C - Reference after dereference terminology

This question is about terminology. int main() { unsigned char array[10] = {0}; void *ptr = array; void *middle = &ptr[5]; // <== dereferencing ‘void *’ pointer } Gcc emits the ...
Bilow's user avatar
  • 2,214
2 votes
0 answers
344 views

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 ...
Paul J. Lucas's user avatar
6 votes
2 answers
423 views

Is accessing members through offsetof well defined?

When doing pointer arithmetic with offsetof, is it well defined behavior to take the address of a struct, add the offset of a member to it, and then dereference that address to get to the underlying ...
Ben Steffan's user avatar
  • 1,105
0 votes
0 answers
287 views

Is there a way to implement offsetof for non-POD types without UB?

For a type T and it's member variable m, the simplest form of offsetof(T, m) would be &static_cast<T*>(0)->m although it is clearly UB. Is there a way to do the same thing without ...
Chungmin Lee's user avatar
  • 2,330
5 votes
3 answers
2k views

Struct offsets and pointer safety in C++

This question is about pointers derived using pointer arithmetic with struct offsets. Consider the following program: #include <cstddef> #include <iostream> #include <new> struct ...
Chris Beck's user avatar
  • 15.7k
0 votes
1 answer
1k views

Offsets of structure members at the compile time or using IDE

How can I know how all members of the structure are located inside? I need detailed listing with all the offsets and sizes Is there's any plugin for IDE, I use Visual Studio 2013? I can't use offset ...
Annett's user avatar
  • 75
-2 votes
2 answers
96 views

x macro prints wrong offsetof() information

I am stumped by an apparent error in my use of offsetof() within an X macro. The code below shows two examples of a rather simple structure, one defined explicitly, and another defined using an X ...
Paul Grinberg's user avatar
1 vote
2 answers
264 views

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 { ...
Marc's user avatar
  • 42
4 votes
2 answers
2k views

How to get the offset of a nested struct member in C?

One solution to print the offset of the checksum field in the info struct, is to use the macros typeof and offsetof: #include <stdio.h> #include <stddef.h> #include <stdint.h> ...
nowox's user avatar
  • 26.7k
0 votes
1 answer
802 views

Coverity scan error RW.UNDEFINED_IDENTIFIER when offsetof() is used

Coverity detects an undefined identifier while trying to calculate offset of a member in the structure. typedef struct A { uint8_t mem[10]; } A; size_t offset = offsetof(A, mem); // This line ...
0sn1s's user avatar
  • 1
6 votes
4 answers
3k views

How to set structure element at desired offset

In embedded programming when describing the hardware one often needs to place struct elements at known predefined positions as the HW engineer designed them. For example, let's define a structure FPGA,...
leonp's user avatar
  • 487
10 votes
2 answers
474 views

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 ...
skyking's user avatar
  • 13.9k
5 votes
1 answer
707 views

Difficulty in understanding the offsetof MACRO

I have been searching very long and hard (links at the very end) for an explanation of the implementation of the offsetof MACRO : #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) ...
maverick's user avatar
9 votes
3 answers
953 views

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 <stddef....
user avatar
2 votes
1 answer
707 views

Getting the offset of a member variable via casting a nullptr

I'm looking at the macro offsetof from <cstddef>, and saw that a possible implementation is via #define my_offsetof(type, member) ((void*) &(((type*)nullptr)->member)) I tried it and ...
vsoftco's user avatar
  • 55.7k
2 votes
2 answers
2k views

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;...
user3339566's user avatar
1 vote
1 answer
70 views

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 ...
Museful's user avatar
  • 6,771
0 votes
0 answers
105 views

Calculate member offset of unknown type

I want to get the offset of a struct's member. I know this has been asked multiple times and the answer is always the mighty offsetof. Well, my case is a little different: I need the offset of an ...
Rodolfo's user avatar
  • 335
7 votes
1 answer
453 views

Boost Fusion Types offsetof

I'm currently trying to calculate an offset of a data member in a boost fusion adapted structure, but I am not sure if there is an elegant way to do so. I'd like to do something like the following: #...
user985030's user avatar
  • 1,567
0 votes
2 answers
2k views

Visual Studio Syntax Error on offsetof()

I have type: typedef struct { int x; int y; int z; } sdf_test_t; But when I try to compile the following: offset = offsetof(sdf_test_t, z); Visual Studio responds with: c:\dataflash.c(...
NZD's user avatar
  • 1,790