3 ms·
This is a really interesting paper which I haven't been able to finish in detail yet. Just a couple of quick reactions: > We would like to be able to make a c
by haberman 12y ago
This is a really interesting paper which I haven't been able to finish in detail yet. Just a couple of quick reactions:
> We would like to be able to make a const pointer a
guarantee that nothing that receives the pointer may write
to the resulting memory. This allows const pointers to be
passed across security-domain boundaries.
I've always wished that were the case too. It would also allow the compiler more optimization opportunities, because it could assume that a call to f(&x) will not change x unless a non-const pointer to x has escaped.
> Container describes behavior in a macro common in the
Linux, BSD, and Windows kernels that, given a pointer
to a structure member, returns a pointer to the enclosing
structure [20]. This may or may not be permitted behavior
according to the standard, due to the ambiguous definition
of ‘object’.
I'm not sure what interpretation of the C standard would disallow this. For the initial member of a structure it seems particularly clear-cut that it is allowed:
C99, 6.7.2.1.13:
"A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning."
This seems to pretty unambiguously state that you can convert a pointer to an initial member to a pointer to the enclosing struct. I can't imagine what interpretation of the standard would disallow this, or even disallow using offsetof() to convert a non-first member to a pointer to the struct.
- Someone 12y agoTo get from a pointer to member to a pointer to an object, you have to subtract the offset of the member within the struct from the pointer you have. The point of discussion is that there is no portable way to write the 'offsetof' macro, even though every compiler on the planet (AFAIK, YMMV) works fine with #define offsetof(st, m) ((size_t)(&((st *)0)->m)) See http://en.m.wikipedia.org/wiki/Offsetof#Implementation http://en.m.wikipedia.org/wiki/Offsetof#Implementation, http://stackoverflow.com/questions/6700114/portability-of-using-stddef-hs-offsetof-rather-than-rolling-your-own http://stackoverflow.com/questions/6700114/portability-of-us... Long story short: use what your compiler provides in stddef.h, and you'll be fine. If it doesn't provide offsetof, migrate to a modern ISO C compiler, or copy the definition used above, and test.
- arielby 12y agoThe C standard guarantees Pointer to Struct ~ Pointer to (transitively)-initial Member (through casts, either directly or via `void*` or via `intptr_t`). It doesn't guarantee Pointer to Struct + offset ~ Pointer to non-initial Member for any value of offset.
- haberman 12y agoSo you can easily use &struct_var->member to go from pointer-to-struct to pointer-to-member. It sounds like you are claiming that there is no way to go back.