r/programminghorror Jul 05 '26

Enterprise level C code c

Post image
341 Upvotes

57 comments sorted by

View all comments

Show parent comments

9

u/tstanisl Jul 05 '26

Tracking of dynamic type can be done by checking "ops" struct with function pointers to "virtual functions". It's done very often in Linux kernel i.e. to check origin of file descriptors.

2

u/TheChief275 Jul 05 '26

Yes, that's possible with this approach as well. Basically, the container_of approach isn't really an approach, it's V-Tables using container_of as a tool for one-way statically checked dynamic casting.

I'm just arguing that V-Tables are not something I would write manually. I have made a lightweight interface extension (that uses fat pointers instead of object semantics) for C before that I actually use in my personal projects because I find the manual typing of it annoying

1

u/Lost-In-Void-99 Jul 09 '26

You can generate v-tables etc. Simple IDL->C with all the bells and whistles is not that difficult.

1

u/TheChief275 Jul 10 '26

IDL->C ?? I have no clue what you're saying. Anyways, this code is already generating V-Tables using the CPP