4 ms·
More importantly offsetof is an implementation-specific macro. Whatever it does cannot be generalized from its definition for one or many compilers to a languag
by potiuper 8y ago
More importantly offsetof is an implementation-specific macro. Whatever it does cannot be generalized from its definition for one or many compilers to a language requirement. The use of the offsetof macro in a program may cause undefined behavior when compiled with GCC or Clang. GCC instead provides __builtin_offsetof. If multiple lists are desired, then an array of them should be declared at the head of the struct and the index be fixed to the type of list desired to contain a reference to the struct. Additionally, as rustyrussell wrote in another comment the list head and member structs should be different in order to optimize the list struct as CCAN's list is implemented, but it too uses offsetof, which leads to the same issue of undefined behavior.
Ref:
https://en.wikipedia.org/wiki/Offsetof https://en.wikipedia.org/wiki/Offsetof
https://stackoverflow.com/questions/26906621/does-struct-name-null-b-cause-undefined-behaviour-in-c11 https://stackoverflow.com/questions/26906621/does-struct-nam...
- rkangel 8y agoThis is not true. offsetof is defined in the C standard. See http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf, Section 7.17, point 3 on page 254. I prefer not to use it due to the loss of type information and the potential for screwups, but you can write ANSI portable C using it.
- potiuper 8y agoThe Offsetof macro is vaguely "defined" in the C standard, but the implementation of the macro is not explicit. So, I do not understand why the original comment is not true?
- rkangel 8y agoThe standard I linked describes both the type signature, and also what the macro should do. There are various ways that that can be implemented by the compiler, but assuming that you are using a compiler that meets the ANSI C standard, then the macro will behave as the standard says it should. Using __builtin_offsetof is undefined, because it is not standardised. The fact that __builtin_offsetof is used to implement offsetof by GCC does not affect the fact that offsetof itself is standardised (in the same way that a given internal heap operation is not standardised, but might be used by GCC to implement malloc which is).
- deleted 8y ago[deleted]