4 ms·
The thing I miss most in C when I don't cheat and use a couple C++ features is templates. Specifically, a dynamically sized List implementation that is type-gen
by AndyKelley 11y ago
The thing I miss most in C when I don't cheat and use a couple C++ features is templates. Specifically, a dynamically sized List implementation that is type-generic.
If you do this in pure C, you have to pick your poison:
1. preprocessor abuse
2. void *
3. multiple redundant implementations of the data structure
Dynamically sized lists are used so often, that this tends to be a problem in almost every C project. I wonder what the author's solution is to this dilemma.
- zik 11y agoThe trade-off with templates is much slower compilation times. Often slow C++ compile times can be traced to enthusiastic use of templated types, particularly given that standard types like std::string are often defined as templates themselves.
- jpfr 11y agoJust use queue.h, as do all the BSDs. https://github.com/freebsd/freebsd/blob/master/sys/sys/queue.h https://github.com/freebsd/freebsd/blob/master/sys/sys/queue...
- dottrap 11y agoThere is also STB stretchy buffer, popular with game devs. https://github.com/nothings/stb/blob/master/stretchy_buffer.h https://github.com/nothings/stb/blob/master/stretchy_buffer....
- krylon 11y agoI think it's GPL, so it's not for everyone, but the last time I did something in C, I came across Judy, which is totally awesome for what it does. Apparently, the implementation is complex enough to drive an ordinary mortal like myself insane, but it is hidden behind a very simple interface, and it is blazingly fast. It only gave me problems when trying to use boehmgc, because it does not detect pointers stored in a Judy tree.
- abcd_f 11y agoBAH! 4. The "intrusive" containers, but of course. The one true way to do it in C. For one, you can keep items in multiple containers, all being on equal footing. None of that typical C++ mess, when this list is a primary storage for items, and those maps are secondary indexes, all sprinkled evenly with iterators. Intrusive containers don't own items, they merely organize them, which is exactly the right way to go about it. For two, the actual code for container operations is as abstract and on-point as it gets as it focuses solely on "weaving" items into and out of a container rather than on other things, like de/allocating supporting structures. For three, you get no heap activity when adding/removing items to/from a container. This means that if you already have the items, you can always arrange them into a collection. This also eliminates a lot of error handling code, leading to a simpler code. Obviously, this is not limited to just linked lists. [1] http://www.makelinux.net/ldd3/chp-11-sect-5 http://www.makelinux.net/ldd3/chp-11-sect-5 (
- notalaser 11y agoYes yes! Yes! It saddens me that this extremely useful idiom is not more widely taught. It's a beautiful, simple concept that works for so many things.
- zodiac 11y agoWhat is an intrusive container?
- exDM69 11y agoA technique commonly used in C, for example in the Linux kernel. You embed "links" to other nodes in your structs, and find the struct from the link based on offsetof. Linux kernel uses linked lists and red-black trees this way. Probably other data structures too. Intrusive containers are cache friendly and typically require no dynamic allocations (in addition to allocating the data itself). See the link in the post above for more about linked lists in the linux kernel.
- charlieflowers 11y ago
- Gracana 11y agoMacros are a reasonable way to solve this problem. A good example is BSD's sys/queue.h: http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/sys/queue.h?rev=1.43&content-type=text/x-cvsweb-markup http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/sys/queue.h...
- majika 11y agoMy solution is to write Jinja templates of C source files: https://github.com/mcinglis/libarray https://github.com/mcinglis/libarray Libarray (and my other C libraries; Libvec etc) have served me very well on a 20k LOC project. With ~150 source files, the entire project builds from fresh in 20 seconds on a i7-2620M. Rebuilds are super-fast; with proper Makefile specification, there's no need to rerender/recompile the templated files.
- kungfooman 11y agoAGPL? Never. Ever.
- majika 11y agoMy reasons are made clear in the README. I don't want to contribute to nonfree software. It's pretty simple. Businesses who want to use my work in nonfree software are welcome to get in touch to negotiate such a license. IMHO, for anyone who has seriously considered the morality and ethics of software licensing options, the AGPL is the obvious choice. I'm surprised it isn't more popular. It was a shame large donors strongarmed the FSF into splitting it from the GPL, and that the FSF kept pushing the GPL as it's go-to license.