offsetof

Using offsetof() to get owner object from member variable

孤人 提交于 2019-12-02 09:03:28
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* GetParent() { return m_parent; } }; So I used offsetof() macro (performance costs of calling offsetof() can be

Standard way to find base address of struct from a member

做~自己de王妃 提交于 2019-12-02 02:48:29
struct Data { int a; std::string b; float c; }; std::string* allocateDataAndGetString() { Data* dataPtr(someAllocator.allocate<Data>()); return &dataPtr.b; } Data* getBaseDataPtrFromString(std::string* mStringMember) { // ??? } int main() { std::string* stringPtr(allocateDataAndGetString()); Data* dataPtr(getBaseDataPtrFromString } I have a Data instance allocated on the heap, and a pointer to its std::string b; member. How do I get the base address of the Data instance the string is a member of, taking into account offsets and padding, in a standard way? I've tried subtracting sizeof(int) and

Why subtract null pointer in offsetof()?

牧云@^-^@ 提交于 2019-11-30 08:37:19
问题 Linux's stddef.h defines offsetof() as: #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) whereas the Wikipedia article on offsetof() (http://en.wikipedia.org/wiki/Offsetof) defines it as: #define offsetof(st, m) \ ((size_t) ( (char *)&((st *)(0))->m - (char *)0 )) Why subtract (char *)0 in the Wikipedia version? Is there any case where that would actually make a difference? 回答1: The first version converts a pointer into an integer with a cast, which is not portable. The second

Is apparent NULL pointer dereference in C actually pointer arithmetic?

三世轮回 提交于 2019-11-29 07:16:09
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. But how does it work? This is not an "and", this is taking the address of the right hand side

Why subtract null pointer in offsetof()?

与世无争的帅哥 提交于 2019-11-29 07:13:40
Linux's stddef.h defines offsetof() as: #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) whereas the Wikipedia article on offsetof() ( http://en.wikipedia.org/wiki/Offsetof ) defines it as: #define offsetof(st, m) \ ((size_t) ( (char *)&((st *)(0))->m - (char *)0 )) Why subtract (char *)0 in the Wikipedia version? Is there any case where that would actually make a difference? The first version converts a pointer into an integer with a cast, which is not portable. The second version is more portable across a wider variety of compilers, because it relies on pointer arithmetic by

Determining struct member byte-offsets at compile-time?

£可爱£侵袭症+ 提交于 2019-11-29 06:39:01
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 to normal is (in this case it should be 12 .) I know that I could use offsetof , but that is a run-time function and I'd prefer not to use it. Is what I'm trying to accomplish even possible? EDIT : offsetof is compile-time, my bad! offsetof is a compile time constant, if we look at the draft C++ standard section C.3 C standard library paragraph 2 says: The C++ standard library provides 57 standard macros

offsetof at compile time

谁说我不能喝 提交于 2019-11-28 08:40:55
Is there a way of finding the offset of a member of a structure at compile-time? I wish to create a constant containing the offset of a structure member. In the following code the offsetof() macro works in the first printf statement. However, the use in line 10 to declare ofs generates the error: "Cannot resolve '->' operator as a constant expression". Is there any other way of doing it? struct MyStruct { unsigned long lw; unsigned char c[5]; int i; int j; unsigned long last; }; const int ofs = offsetof(struct MyStruct, i); // This line in error int main(void) { printf("Offset of c = %d.\n",

Determining struct member byte-offsets at compile-time?

北城余情 提交于 2019-11-28 00:05:20
问题 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 to normal is (in this case it should be 12 .) I know that I could use offsetof , but that is a run-time function and I'd prefer not to use it. Is what I'm trying to accomplish even possible? EDIT : offsetof is compile-time, my bad! 回答1: offsetof is a compile time constant, if we look at the draft C++

offsetof at compile time

匆匆过客 提交于 2019-11-27 02:22:31
问题 Is there a way of finding the offset of a member of a structure at compile-time? I wish to create a constant containing the offset of a structure member. In the following code the offsetof() macro works in the first printf statement. However, the use in line 10 to declare ofs generates the error: "Cannot resolve '->' operator as a constant expression". Is there any other way of doing it? struct MyStruct { unsigned long lw; unsigned char c[5]; int i; int j; unsigned long last; }; const int ofs

Does &((struct name *)NULL -> b) cause undefined behaviour in C11?

泄露秘密 提交于 2019-11-26 18:48:01
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 doesn't define the term "dereference". 6.5.3.2/4 clearly says that using * on a null pointer causes undefined behaviour; however it doesn't say the same for -> and also it does not define a -> b as being (*a).b ; it has separate definitions for each operator. The semantics of -> in 6.5.2.3/4 says: A postfix expression followed by the -> operator and an identifier designates a member of a structure or union object.