offsetof Questions

2

Solved

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> #inc...
Soembawa asked 24/4, 2022 at 23:45

2

Solved

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; return 0; } ...
Carpo asked 29/10, 2021 at 16:13

8

Solved

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...
Excommunication asked 3/4, 2009 at 13:43

4

Solved

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 F...
Fowle asked 14/8, 2016 at 12:13

1

Solved

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...
Federico asked 17/9, 2019 at 17:6

1

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 ari...
Cranial asked 8/4, 2019 at 17:8

6

Solved

Example from MSVC's implementation: #define offsetof(s,m) \ (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m)) // ^^^^^^^^^^^ As can be seen, it dereferences a null...
Lament asked 21/6, 2011 at 23:48

11

Solved

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 t...
Mathematics asked 15/7, 2009 at 7:21

1

Solved

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]: conditional...
Pentateuch asked 27/11, 2017 at 19:37

1

Solved

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 volatile&...
Bashee asked 10/11, 2017 at 9:53

2

Solved

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 underlyin...
Godesberg asked 2/10, 2017 at 10:52

3

Solved

This question is about pointers derived using pointer arithmetic with struct offsets. Consider the following program: #include <cstddef> #include <iostream> #include <new> stru...
Reason asked 8/9, 2017 at 20:9

2

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 con...
Bacteria asked 24/5, 2016 at 11:57

1

Solved

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)->MEMB...
Competency asked 11/4, 2016 at 1:55

3

Solved

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 <stdd...
Amphibolous asked 7/2, 2016 at 21:14

1

Solved

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:...
Seaware asked 25/8, 2015 at 13:53

6

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 doe...
Pew asked 13/11, 2014 at 10:27

3

Solved

I found this code sample in a book, but I am unable to understand the expression in printf statement. and this program compiles successfully giving output as 4. kindly advise... void main(){...
Ex asked 13/11, 2014 at 9:10

1

The code below is here: https://ideone.com/XnxAyw The compiler error I get is: prog.cpp: In member function ‘size_t list_base<T, NODE, true>::offset()’: prog.cpp:26:22: error: expected unqu...
Hatchery asked 12/3, 2014 at 17:37

3

Solved

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 ...
Invigorate asked 13/11, 2013 at 0:55

3

Solved

I have a struct defined as: struct smth { char a; int b[]; }; When I call sizeof and offsetof on this struct: cout << sizeof(struct smth) << endl; cout << offsetof(struct sm...
Brogue asked 17/8, 2013 at 15:13

7

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 mater...
Claman asked 7/10, 2008 at 10:33

1

Solved

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 <typename T, typename R, R T::*M > constexpr...
Wendel asked 10/10, 2012 at 2:56

1

I am writing an OpenGL program using the GLM OpenGL maths library. I would like to combine vertex positions, normals and texture coordinates into one class like so class Vertex { public: glm::ve...
Carlina asked 22/9, 2012 at 23:20

4

Solved

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 A...
Sexivalent asked 8/2, 2011 at 18:45

© 2022 - 2024 — McMap. All rights reserved.