offsetof

Does the &#39;offsetof&#39; macro from <stddef.h> invoke undefined behaviour?

為{幸葍}努か 提交于 2019-11-26 15:31:09
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 pointer, which normally invokes undefined behaviour. Is this an exception to the rule or what is going on? Where the language standard says "undefined behavior", any given compiler can define the behavior. Implementation code in the standard library typically relies on that. So there are two questions: (1) Is the code UB with respect to the C++ standard? That's a really hard question, because it's a well known almost

Why does this implementation of offsetof() work?

柔情痞子 提交于 2019-11-26 12:22:21
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 a NULL pointer? Or is this some sort of compiler hack where it sees that only address of the offset is taken out, so it statically calculates the address without actually dereferencing it? Also is this code portable? JaredPar At no point in the above code is anything dereferenced. A dereference occurs when the * or -> is used on an address value to find referenced value. The only use of * above is in a type

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

僤鯓⒐⒋嵵緔 提交于 2019-11-26 06:36:57
问题 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

Why can&#39;t you use offsetof on non-POD structures in C++?

a 夏天 提交于 2019-11-26 05:32:02
问题 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 that are not Plain Old Data Structures. I tried it out and it seems to work fine. class Foo { private: int z; int func() {cout << \"this is just filler\" << endl; return 0;} public: int x; int y; Foo* f; bool returnTrue() { return false; } }; int main() { cout << offsetof(Foo, x) << \" \" << offsetof

Does the &#39;offsetof&#39; macro from <stddef.h> invoke undefined behaviour?

混江龙づ霸主 提交于 2019-11-26 04:28:06
问题 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 pointer, which normally invokes undefined behaviour. Is this an exception to the rule or what is going on? 回答1: Where the language standard says "undefined behavior", any given compiler can define the behavior. Implementation code in the standard library typically relies on that. So there are two questions: (1) Is

Why does this implementation of offsetof() work?

为君一笑 提交于 2019-11-26 02:56:47
问题 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 a NULL pointer? Or is this some sort of compiler hack where it sees that only address of the offset is taken out, so it statically calculates the address without actually dereferencing it? Also is this code portable? 回答1: At no point in the above code is anything dereferenced. A dereference occurs when the