4 ms·
The type in question is the containing C struct type. Think of sgl_baseof as the opposite of offsetof. I didn't come up with it myself of course, it's probably
by sifoobar 8y ago
The type in question is the containing C struct type. Think of sgl_baseof as the opposite of offsetof. I didn't come up with it myself of course, it's probably older than I am; I borrowed it from the Linux kernel. The other link is an example of how it's used.
- deleted 8y ago[deleted]
- potiuper 8y agosql_baseof as shown is defined in the header using offsetof. offsetof is an implementation-specific macro. Whatever it does is specific to the compiler that provides it. You cannot generalize from its definition for one or many compilers to a language requirement. https://stackoverflow.com/questions/26906621/does-struct-name-null-b-cause-undefined-behaviour-in-c11 https://stackoverflow.com/questions/26906621/does-struct-nam...
- sifoobar 8y agoI fail to see your point here. sgl_baseof uses offsetof with a valid struct type and a valid field name, so where is the UB? Unless you feed it an invalid pointer, but then all bets are off.
- sifoobar 8y agoSorry, still not getting it. If an implementation provides offsetof and it doesn't work, that implementation would go out of fashion in a heart beat. Where is the practical use of this information? How is this even an argument?
- potiuper 8y agoThe use of the Offsetof macro in a program will cause undefined behavior when compiled with GCC or Clang and they are still "in fashion" as far as I am aware. The practical use of this information is to prevent the occurrence of undefined behavior by knowing not to use offsetof for example with GCC and instead use __builtin_offsetof in the specific case of GCC. The argument appears to be that you are claiming that what sgl_baseof does is not undefined behavior and I am claiming that it is not possible to infer that without specifying the implementation of the offsetof macro.
- potiuper 8y agohttps://en.wikipedia.org/wiki/Offsetof https://en.wikipedia.org/wiki/Offsetof repeats what is said the SO link and might be helpful? The occurrence of the UB is not dependent on the parameters that sgl_baseof is called with. The UB may occur solely based on the compiler and standard library implementation. If the UB does occur based on those two and offsetof is not redefined somewhere, then the UB occurs anywhere offsetof is used.