开发者

High Level Data Structures in C [duplicate]

开发者 https://www.devze.com 2023-03-21 06:41 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: C Analog To STL Oftentimes, when programming in C, I find myself wishing that I had access to something lik
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

C Analog To STL

Oftentimes, when programming in C, I find myself wishing that I had access to something like the vector or list classes from C++, and I end up implementing a sort of stripped-down version of some high-level data structure, b开发者_运维技巧ut it takes a fair amount of time. I am wondering if anyone knows of a good library of high quality simple data structures in C?


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


Other than GLib as others have suggested, you could create your own generic types without any type safety, such as a linked list that uses void * as the generic pointer to the next stack object. Some have even used macros to create template-like behaviour in ANSI C with some reasonable success. Search around cplusplus.com's forums for an example or two (there aren't many more than that if I recall correctly).

Alternatively you could implement your own OOP scheme as described in the paper Object-Oriented Programming With ANSI C, though it's something I wouldn't recommend. You might as well take the time you'd spend creating that beast (and possibly not understanding how it works in the end) and learn some of GLib to get familiar with it.

Personally I prefer the first approach. You lose type safety and gain a bunch of casts, but it's the fastest to implement. The real problem with that method is the data that is stored. std::stack<int> converted to C is simply a struct stack that has a data field of type int. What about std::vector<std::stack<int> >, an addressable list of stacks of integers? The C standard cannot guarantee that casting from a pointer type to an integral type will work without some loss of information. In other words, while you can necessarily use pointers, you can't use plain integers to store information. Perhaps a few unrolled template specializations would work -- one for int, (one for long long?), one for double, and one for pointer types.

In the end, the best solution is to use a library like GLib, but if you prefer to roll your own, take care when creating it. You never know when you'll need a combination of types of items in a container.


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

You may also want to take a look at Rusty Russell's CCAN project. I haven't used any code from it but I will definitely go dip my toe in the next time I have similar needs:

http://ccan.ozlabs.org/

The Idea

That nice snippets of C code should be moved out of junkcode directories and exposed to a wider world, where they can become something useful.

CCAN is loosely modelled after the successful CPAN project for Perl code development and sharing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号