High Level Data Structures in C

Possible duplicate:
C Analog To STL

Often when programming in C, I find myself wanting to have access to something like vector classes or lists from C ++, and I end up implementing a kind of stripped-down version of some high-level data structure, but it takes a lot of time. I am wondering if anyone knows of a good library of quality simple data structures in C?

+4
source share
3 answers

Well, there is a GLib library that is used to implement the GTK + widget suite. GLib uses GObject as a way to do OOP in C. But GObjectified C is famous for being very verbose.

+5
source

Besides GLib, as others have suggested, you can create your own generic types without any type safety, like a linked list that uses void * as a generic pointer to the next stack object. Some of them even used macros to create template behavior in ANSI C with some reasonable success. Find an example or two in the cplusplus.com forums (there aren’t so many, if I remember correctly).

Alternatively, you could implement your own OOP scheme, as described in Object Oriented Programming with ANSI C , although I would not recommend this. You could also spend the time spent creating this beast (and maybe not understand how it works in the end) and study some of GLib to familiarize yourself with it.

Personally, I prefer the first approach. You lose type safety and get a lot of castings, but this is the fastest way to implement. The real problem with this method is the data that is being stored. std::stack<int> , converted to C, is just a structure stack that has an int data field. What about std::vector<std::stack<int> > , an address list of stacks of integers? C standard cannot guarantee that casting from pointer type to integral type will work without any loss of information. In other words, although you can use pointers, you cannot use prime integers to store information. Perhaps, several expanded specialized templates will work - one for int (one for long?), One for double and one for pointer types.

In the end, the best solution is to use a library such as GLib, but if you prefer to roll on your own, take care to create one. You never know when you will need a combination of item types in a container.

+1
source

I like GLib for this kind of thing; the code you finish writing is clean, the libraries are easy to use, stable, mature and cross-platform, and the documentation is great.

You can also take a look at the Rusty Russell CCAN project. I have not used any code from it, but I will definitely go to plunge the next time when I have similar needs:

http://ccan.ozlabs.org/

Idea

Good pieces of C code should be moved from junkcode directories and open to a wider world where they can be of some use.

CCAN is freely modeled after a successful CPAN project for developing Perl code and sharing.

0
source

All Articles