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
1
vote
1 answer

Using offsetof macro within custom macro

I am trying to use the offsetof macro within another macro of my own, as follows: #define MY_MACRO(struct_type, member) \ my_function(param1, offsetof(struct_type, member)) When I use my custom macro, the compiler complains that 'member' has…
1
vote
1 answer

Get address of a non-POD object from within a data member, which is a single-use nested class

I'll start with some code: class myNonPODClass { public: virtual ~myNonPODClass() {} class { public: myNonPODClass* GetContainer() { return (myNonPODClass*)((int8_t*)(this) - offsetof(myNonPODClass,…
Brent
  • 4,153
  • 4
  • 30
  • 63
1
vote
3 answers

How to do a 'memcpy' between different structures using 'offsetof'?

I have the following two structures. I need to copy d, e, f from source to destination using memcpy and offsetof. How can I do this? struct source { int a; int b; int c; int d; int e; int f; }; struct destination { int…
Sandeep
  • 69
  • 1
  • 9
0
votes
1 answer

Pointer-to-member, type descriptors and references

I'm working on a type descriptor project in C++11. The type descriptor's job is to know the types of every member in a class, it's size and it's offset from the base of an object. I don't support multiple inheritance as well as objects with virtual…
fronsacqc
  • 320
  • 2
  • 12
0
votes
1 answer

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 like to get same sum Every time for CVN…
Mehul
  • 3
  • 2
0
votes
0 answers

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 __typeof__(((type *)0)->member)* mptr = (ptr);\ …
Uri Raz
  • 435
  • 1
  • 3
  • 15
0
votes
0 answers

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 example, consider the code below struct car //car is…
0
votes
0 answers

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 struct XLogRecord { uint32 xl_tot_len; …
lumos
  • 1
  • 3
0
votes
1 answer

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): name = "xyz" and (id = 10 or type = 5) I have…
psy
  • 914
  • 3
  • 10
  • 31
0
votes
1 answer

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 the code it runs. (For example when a variable is…
user3520616
  • 60
  • 3
  • 17
0
votes
0 answers

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, but it doesn't seem to work for me. The other…
0
votes
1 answer

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 behavior because it may be dereferencing a pointer. I…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
0
votes
1 answer

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 #include #include struct test { int x, y, z; }; int main() { …
Gian
  • 327
  • 2
  • 8
0
votes
3 answers

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 { int a = 0, b = 0; }; struct Inner2 { int…
mihaild
  • 280
  • 3
  • 11
0
votes
0 answers

Using in prog_kern.c ebpf

I've seen that instead of using #include 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 prog_kern.c? If yes, why should I avoid it?
Maicake
  • 1,046
  • 10
  • 34