offsetof

Is apparent NULL pointer dereference in C actually pointer arithmetic?

﹥>﹥吖頭↗ 提交于 2019-12-18 05:01:54
问题 I've got this piece of code. It appears to dereference a null pointer here, but then bitwise-ANDs the result with unsigned int . I really don't understand the whole part. What is it intended to do? Is this a form of pointer arithmetic? struct hi { long a; int b; long c; }; int main() { struct hi ob={3,4,5}; struct hi *ptr=&ob; int num= (unsigned int) & (((struct hi *)0)->b); printf("%d",num); printf("%d",*(int *)((char *)ptr + (unsigned int) & (((struct hi *)0)->b))); } The output I get is 44

Is apparent NULL pointer dereference in C actually pointer arithmetic?

拜拜、爱过 提交于 2019-12-18 05:01:43
问题 I've got this piece of code. It appears to dereference a null pointer here, but then bitwise-ANDs the result with unsigned int . I really don't understand the whole part. What is it intended to do? Is this a form of pointer arithmetic? struct hi { long a; int b; long c; }; int main() { struct hi ob={3,4,5}; struct hi *ptr=&ob; int num= (unsigned int) & (((struct hi *)0)->b); printf("%d",num); printf("%d",*(int *)((char *)ptr + (unsigned int) & (((struct hi *)0)->b))); } The output I get is 44

How do one use `offsetof` to access a field in a standard conforming way?

爷,独闯天下 提交于 2019-12-12 09:34:54
问题 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 conforming way? Assuming of course that we have a correct struct A* and a correct offset. One attempt would be to do something like: int getint(struct A* base, size_t off) { return *(int*)((char*)base + off); } Which probably will work, but note for example that pointer arithmetics only seem to be defined

Use offsetof with GLM (OpenGL maths)

a 夏天 提交于 2019-12-12 03:39:41
问题 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; glm::vec2 texcoord; }; and then use an array of these as my vertex buffer object (VBO). However, when calling glVertexAttribPointer to map my VBOs I need to give it an offset into this combined Vertex struct for the normal and texcoord members. Had these just been PODs I

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

删除回忆录丶 提交于 2019-12-11 12:33:41
问题 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 unqualified-id before ‘*’ token return offsetof(T, *NODE); ^ prog.cpp:26:22: error: expected ‘)’ before ‘*’ token Visual Studio is OK with using offsetof(type, 'pointer to member') but is that because it's lax? If so, does anyone know a standards compliant way to use offsetof() with a pointer to member template

About offsetof usage in C and C++?

我怕爱的太早我们不能终老 提交于 2019-12-10 19:53:51
问题 In C, I saw some usage of offsetof to calculate the offset of a member in the structure to its beginning? Is it still recommended to use in C++? Any other way to do this without this macro? 回答1: C++ has pointers to members. These are similar to offsets, but (1) typesafe and (2) more generic - they also work with methods, on base classes, etc. 回答2: Offsetof is a feature that is only in the C++ standard for C compatibility so it is not reccomended to be used in C word 回答3: I wouldn't recommend

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

﹥>﹥吖頭↗ 提交于 2019-12-04 19:20:28
I'll start with some code: class myNonPODClass { public: virtual ~myNonPODClass() {} class { public: myNonPODClass* GetContainer() { return (myNonPODClass*)((int8_t*)(this) - offsetof(myNonPODClass, member)); } } member; }; Obviously, this is a contrived example. The code compiles fine, but I'm worried about the "Offset of on non-POD type 'myNonPODClass'". Is there a better way to do essentially the same thing WITHOUT having to pass the myNonPODClass pointer into the nested anonymous classes constructor (or similar)? "member" must be ready to go without any initialization. Is it possible?

Do we need to use std::launder when doing pointer arithmetic within a standard-layout object (e.g., with offsetof)?

社会主义新天地 提交于 2019-12-03 05:38:36
问题 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 arithmetic within a standard-layout object using an unsigned char pointer. This would appear to imply that some code similar to that in the linked question should work as intended: struct Foo { float x, y, z; }; Foo f; unsigned char *p = reinterpret_cast<unsigned char*>(&f) + offsetof(Foo, z); // (*) *reinterpret

Do we need to use std::launder when doing pointer arithmetic within a standard-layout object (e.g., with offsetof)?

纵然是瞬间 提交于 2019-12-02 18:58:13
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 arithmetic within a standard-layout object using an unsigned char pointer. This would appear to imply that some code similar to that in the linked question should work as intended: struct Foo { float x, y, z; }; Foo f; unsigned char *p = reinterpret_cast<unsigned char*>(&f) + offsetof(Foo, z); // (*) *reinterpret_cast<float*>(p) = 42.0f; (I have replaced char with unsigned char for greater clarity.) However, it seems

Using offsetof() to get owner object from member variable

我的未来我决定 提交于 2019-12-02 14:19:30
问题 I would like to implement 'GetParent()' function in here- class ChildClass; class ParentClass { public: .... ChildClass childObj; .... }; class ChildClass { friend class ParentClass; private: ChildClass(); public: ParentClass* GetParent(); }; I've tried to create a private member variable which stores pointer to parent object. However this method requires additional memory. class ChildClass { friend class ParentClass; private: ChildClass(); ParentClass* m_parent; public: ParentClass*