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
0
votes
0 answers

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(0)->m although it is clearly UB. Is there a way to do the same thing without involving UB on the assumption that T is either POD or non-POD but…
Chungmin Lee
  • 2,320
  • 2
  • 18
  • 19
0
votes
1 answer

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 of or something similar because I need information…
Annett
  • 75
  • 1
  • 7
0
votes
1 answer

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 raises the error. The exact coverity description is…
0sn1s
  • 1
  • 1
0
votes
0 answers

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 unknown type. That is for example: void…
Rodolfo
  • 335
  • 1
  • 4
  • 10
0
votes
2 answers

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(542) : error C2143: syntax error : missing ')'…
NZD
  • 1,780
  • 2
  • 20
  • 29
0
votes
2 answers

How to portably allocate space for a particular member of a union embedded in a struct

Consider the following type in C11, where MyType1 and MyType2 are previously declared types: typedef struct { int tag; union { MyType1 type1; MyType2 type2; } } MyStruct; I'd like to allocate enough memory using malloc to hold the tag…
Marc
  • 4,327
  • 4
  • 30
  • 46
0
votes
1 answer

sizeof a structure vs the offset of last element of the structure

I have a C structure comprising of a couple of elements. When i type offsetof() on the last element of the structure, it shows 144 and sizeof() the last element is 4. So, I was assuming the size of the structure is 148. However, running sizeof() on…
learn_develop
  • 1,735
  • 4
  • 15
  • 33
0
votes
1 answer

getting offset of struct member crash

As far as i know "offsetof" macro is defined as : #define offsetof(st, m) ((size_t)(&((st *)0)->m)) based on this link : http://en.wikipedia.org/wiki/Offsetof So I write my own code snippet to calculate offset of a struct members: typedef struct{ …
0
votes
4 answers

Have C++11 some portable and effective way to access enclosing class from nested class?

What I am needing can be done by storing this pointer of enclosing class into nested class for example this way: class CEnclosing { public: class CNested : public CSomeGeneric { public: CNested(CEnclosing* e) : m_e(e) {} …
user3123061
  • 757
  • 5
  • 14
0
votes
1 answer

Macro with typecasting in C, Learning offsetof in detail

The following code and its output: #include int x = 0; #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)x)->MEMBER) #define offsetof_1(TYPE, MEMBER) ((size_t) &((TYPE *)1)->MEMBER) #define offsetof_2(TYPE, MEMBER)…
Anup Buchke
  • 5,210
  • 5
  • 22
  • 38
0
votes
1 answer

Use offsetof with GLM (OpenGL maths)

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::vec4 position; glm::vec4 normal; …
Kasper Peeters
  • 1,580
  • 2
  • 18
  • 37
-1
votes
1 answer

C programming, error in getting value using container_of() macro

I have created the following code to understand offsetof() and container_of() macros. Here the printf() show two different address instead of same address. What am I doing wrong? #include #include typedef unsigned char…
Dinesh
  • 1,825
  • 5
  • 31
  • 40
-2
votes
2 answers

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 macro. The offsets of each structure field within…
Paul Grinberg
  • 1,184
  • 14
  • 37
1 2 3 4 5
6