5 ms·
[1] is #define[d] as sgl_baseof(type, field, ptr) that appears to used to return a pointer to a "type", which is a value stored in memory at run-time, of a fiel
by potiuper 8y ago
[1] is #define[d] as sgl_baseof(type, field, ptr) that appears to used to return a pointer to a "type", which is a value stored in memory at run-time, of a field in an object (ptr)? I and many others would not consider a run-time type system to be "the" solution to downcasting in C. Regarding the use of C for "fancy abstractions" such as classes and objects, the derivatives of Cfront (Corneau C/C++), which is an implementation of C++ using C, are not just alive, but have been chosen to implement many production systems (https://en.wikipedia.org/wiki/Edison_Design_Group https://en.wikipedia.org/wiki/Edison_Design_Group) used by millions today, although they too "inherit" the issues with the C standard. It is true that the GObject system has many problems one of which is the reference counting (sink/sunk) process as requiring a certain memory (de)allocation mechanics as a requirement of the object system is not the most applicable in certain situations.
- DonHopkins 8y ago>requiring a certain memory (de)allocation mechanics as a requirement of the object system is not the most applicable in certain situations To destruct -- to collect. To collect -- perchance to tenure: ay there's the rub! For in that collection of destruction what cycles may come When we have shuffled off this mortal core, Must give us callbacks.
- gpderetta 8y agoYou have it backward, Comeau is a compiler implemented with the EDG front end not the other way around. Also I'm not sure there is any shared history between Comeau and Cfront. Finally, as far as I understand Cfront was a proper C++ compiler that simply targeted C instead of ASM (like many compilers for other languages do ), not some sort of C preprocessor (Cfront was initially bootstrapped with a c-with-classes preprocessor though).
- potiuper 8y agoComeau is a compiler implemented with the EDG front end. If the other way around was some how implied, then it was not the intention. As for Comeau and Cfront, Comeau Computing's CEO, Greg Comeau, provided one of the early ports of cfront to the PC: https://en.wikipedia.org/wiki/Comeau_C/C%2B%2B https://en.wikipedia.org/wiki/Comeau_C/C%2B%2B. Cfront is not just a preprocessor and if the comparison is limited to only C library implementations of objects such as COS, then CPre / C with Classes would be the applicable choice and not Cfront. But, the original point was not meant to exclude "trans"pilers or compilers that output to C such as Genie, Vala, etc.
- sifoobar 8y agoThe 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.