Hacker News new | past | comments | ask | show | jobs | submit login

What is the preferred way to implement generic containers in C?



I'm not an expert, but I was curious too and looked around. Apparently glib is pretty popular, and they just use void* (typedef'd to gpointer) for the type of items in the container: https://developer.gnome.org/glib/2.42/glib-Doubly-Linked-Lis...


There isn't. You don't do generic containers in C; you implement each specific data structure from the ground up to do specifically what you need.

This is both a strength and a weakness.


You can write the containers as a ".x" file, which is just C with all of the type names replaced by a macro name. Then you #include the .x file where needed, #define the type before, then #undef it after. Token pasting in the C preprocessor makes for unique names, so multiple container types can be used in the same namespace.


You don't. Go to the standards committee on bended knee and ask for another awkward addition to the language like <complex.h>.


This is a banal question, flame bait at that.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: