Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Does anyone know of any C container libraries? I am looking for something which gives standard implementations of linked lists, arrays, hash tables etc, much in the same way as the C++ STL does. Key concerns are:
Client code should be able to create containers for multiple different data types without modifying the library.
The interface for creating and using the containers should be intuitive.
I just came across SGLIB while looking for a C implementation of a map/dictionary container. Unfortunately, no map but it seems to include the containers you asked about. I have no idea how good it is.
http://sglib.sourceforge.net.
Sglib is an excellent generic data structures library. The library currently provides generic implementation for:
sorting arrays
linked lists
sorted linked lists
double linked lists
red-black trees
hashed containers
It's very fast. Faster that glib. It's inspired by the Standard Template Library. Download here
Another solution is Attractive Chaos sotware. C macro library:
kbtree.h: efficient B-tree library in C.
khash.h: fast and light-weighted hash table library in C.
kvec.h: simple vector container in C.
Kulesh Shanmugasundaram presents the generic Linux Kernel Linked List and a generic hash table based in the Linux Kernel Linked List.
Sglib and Attractive Chaos sotware and Linux Kernel Linked List are C macro libraries. Using void* to implement generic containers in C may be inefficient. C macros mimic C++ templates and are as efficient as a C++ template.
Chuck Falconer has a decent hash library written in C that includes a C++ interface, click on hashlib.zip on the webpage to download.
Ben Pfaff has very nice and extremely well-documented binary and balanced tree library, GNU libavl, that implements most major tree structures including binary search trees, AVL trees, red-black trees and threaded versions of each.
libavl is licensed under the LGPL (as of version 2.0.3), hashlib is GPL.
I'm not sure what you are looking for as far as arrays and linked lists go as the former is supported directly by the language and the latter is generally trivial enough to implement without warranting a library.
How about ccl? This is a container library which has been (unsuccessfully) proposed for C standard. Maybe it's best fit for you. You can see https://code.google.com/p/ccl/ https://github.com/jacob-navia/ccl.
Enjoy it.
I've been using a library I've been growing from Hanson's "C Interface and Implementations" book. His source is downloadable at
cii book website
Everything is an Abstract Data Type. There is List, Set, Table (map).
#include "queue.h" to get access to the implementations of singly-linked lists, singly-linked tail queues, lists and tail queues.
I found a generic cache for storing arbitrary objects in memory by D. J. Bernstein (http://cr.yp.to/djbdns.html) to be both clean, simple and super fast. Look up cache.h and cache.c in djdns tarball.
Some of the ones that I have heard of (but never used) are
Glib
iMatix Standard Function Library
disparate elements from the Linux kernel headers (e.g. list)
This seems to cover most of the containers and some algorithms. There is also no licensing, all the headers contain - 'code may be used without restriction.' http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=13867&lngWId=3
Related
Do standard C library implementations, especially glibc (the GNU C Library) provide linked lists, stack et al. data structures, or do we have to roll our own?
Thanks.
The C Standard does not provide data structures like linked list and stack.Some compiler implementations might provide their own versions but their usage will be non portable across different compilers.
So Yes, You have to write your own.
The C standard doesn't, glibc however provides lists, tail queues, and circular queues in <sys/queue.h> according to the queue man page those come from BSD and not POSIX.
There are hash tables, binary trees and binary search stuff in glibc.
Those are part of C89, C99 and/or POSIX.1 standards.
Some reason linked list is not there.
More info from man pages: hsearch, tsearch and bsearch
Note: Some of those have bad design. For example: hsearch allows only one hash table per process. The GNU compiler, gcc/glibc, provides reentrant versions hcreate_r, hsearch_r, and hdestroy_r that allow multiple hash tables. See also Stack Overflow's How to use hcreate_r.
As such C does not provide data structures but you can use the glib provided by Gnome
Queue.h ad Tree.h also provides you some Data structures
There's a hash table implementation in POSIX (and GLibc); see the manpages for hcreate/hdestroy/hsearch.
But, as mentioned, using glib is probably the easiest way to save yourself from reimplementing the basic data structure.
As other answers already stated, there isn't a linked list library in the standard library.
I've written one for my own use a while ago. You can freely use it or use the code as reference.
You can find it here: libllist
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Does anyone have tips/resources for how to, in the best way, structure your C code projects? (Different folders etc.) And how do you know when it's good to split code into separate files? And what is an example of a good Makefile?
My project is not that big, but I wanna start to structure my code at an early stage..
Structuring code needs some experience but mostly common sense.
For splitting code, you usually go for readability: conceptually coherent functions/datatypes should go in the same file. You can take c standard library as a good example. It is better to keep your data structure definitions and function declarations in separate headers. This allows you to use the data structures as part of a compilation unit even if you have not defined all the functions.
Files that provide similar functionality should go in the same directory. It is good to avoid deep directory structure (1 level deep is best) as that complicates building the project unnecessarily.
I think Makefiles are OK for small projects, but become unwieldy for bigger ones. For really serious work (if you want to distribute your code, create an installer etc) you may want to look at cmake, scons, etc.
Have a look at the GNU coding standards: http://www.gnu.org/prep/standards/standards.html
Look at the gnu make manual for a simple example Makefile. You can also pick up any opensource project and look at the Makefile. Browsing code repositories in sourceforge.net may be useful.
Read one of the many C coding standards available on the internet and follow one that looks reasonable for your requirements. A few links:
GNU Coding Standards
C Coding Standards at IRAM (pdf)
Indian Hill C Style and Coding Standards
The following books also contain effective guidelines on writing good C code:
The C Programming Language
The Practice of Programming
The Elements of Programming Style
This is sometimes overlooked, but security is an issue in big projects. Here's some advice about how to program securely.
Here is an idiom I like:
Declare structs in a header so that their size is known by client code. Then declare init and deinit functions to the following convention:
The first parameter is a struct foo*.
The return type is a struct foo*.
If they might fail, the last parameter is either int* (simplest), enum foo_error* (if there are several ways it can fail that the calling code might care about) or GError** (if you're writing GLib-style code).
foo_init() and foo_deinit() return NULL if the first parameter is NULL. They also return the first parameter.
Why do it this way? Calling code doesn't have to allocate heap space for the structure, it can go on the stack. If you are allocating it on the heap, though, the following works nicely:
struct foo* a_foo = foo_init(malloc(sizeof(*a_foo)));
if (a_foo == NULL) {
/* Ruh-oh, allocation failure... */
}
free(foo_deinit(a_foo));
Everything works nicely even if a_foo == NULL when foo_deinit is called.
I was wondering if you knew of a robust implementation of a hashtable in C. I'm looking for something other than ghashtable in glib.
Thanks.
I've heard good things about the GLib Hash Table.
In C:
As previously mentioned g_hash_table in gLib:
http://library.gnome.org/devel/glib/stable/glib-Hash-Tables.html
uthash: https://github.com/troydhanson/uthash
khash in klib: https://github.com/attractivechaos/klib
cdada_map in libcdada: https://github.com/msune/libcdada (C API, backend C++)
If you can/want to use C++:
std::map in libstdc++ (look into std::unordered_map too)
As noted before (updated link) sparsehash: https://github.com/sparsehash/sparsehash (look into dense too)
Will this hashtable work? (got the link from the second post of this thread)
Perhaps this one will?
(got the above from a Google search for "hashtable in c", am not a C programmer)
For a hash table I'd use google-sparsehash
PD: I don't know your requirements, but take a look at HDF5, bear in mind it exists just in case.
update
Memory Structures Library (MemSL2), o MemSL2 in another link
it has implementations (one in pure C and wrappers for C++) of structures, for example, AVL trees, threaded trees, ..., and
Hash Tables with Separate Chaining,
Hash Tables with User-Defined Paging
Hash Tables with Dynamic Paging
A simple one in libc, see <hsearch.h> and man hsearch.
Update: just found that you can implement hashtable very easily with the help of hlist from Linux kernel. Take a look at <list.h> in Linux kernel source code for hlist_head/node and their operations.
You might want to look into using the Apache Portable Runtime? It's license is very liberal and it provides a decent hashtable implementation:
http://apr.apache.org/docs/apr/1.3/group__apr__hash.html
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I'm looking for a C library with common reusable data structures like linked lists, hash tables etc. Something like the source distributed with Mastering Algorithms with C (Paperback) by Kyle Loudon.
BSD queue.h has:
SLIST = singly linked list
LIST = doubly linked list
SIMPLEQ = singly linked queue
TAILQ = doubly linked queue
BSD tree.h has:
RB - red-black tree
SPLAY - splay tree
See the queue(3) and tree(3) man pages for details. I really like them because they are pure C macros without dependencies (not even libc). Plus with the BSD license you don't have to worry about any company restrictions w/ GPL.
Gnome provides an excellent library for this, called Glib, with many useful data structures and other utilities as well.
gnulib, the gnu portability library.
It's distributed as source code.
This list is from its module list, which includes a TON of other things. One interesting one is "c-stack: Stack overflow handling, causing program exit."
list
array-list
carray-list
linked-list
avltree-list
rbtree-list
linkedhash-list
avltreehash-list
rbtreehash-list
sublist ( Sequential list data type backed by another list. )
oset (Abstract ordered set.)
array-oset
avltree-oset
rbtree-oset
SGLIB is an excellent generic data-structures library. The library currently provides generic implementations for:
sorting arrays
linked lists
sorted linked lists
double linked lists
red-black trees
hashed containers
It's very fast, faster than glib. It's inspired by the Standard Template Library. Download Here
Another solution is Attractive Chaos software.
C macro library:
kbtree.h: efficient B-tree library in C.
khash.h: fast and light-weighted hash table library in C.
kvec.h: simple vector container in C.
Sglib and Attractive Chaos software are C macros library. Using void* to implement generic containers in C may be inefficient. C macros mimics C++ template and are as efficient as C++ template
The GDSL Library might be a good thing to consider:
http://home.gna.org/gdsl/
The Apache Portable Runtime.
CLIB
AT&T's software tools.
Just because I'm curious--is there any C analog to the functionality of the STL in C++? I've seen mention of a GTK+ library called glib that a few people consider fills the bill but are there other libraries that would provide STL functionality in C?
Yes, glib is a pretty good choice: it includes a lot of utilities for manipulating containers like linked lists, arrays, hash tables, etc. And there is also an object-oriented framework called GObject that you can use to make objects with signals and slots in C (albeit with rather verbose function call names like gobject_set_property, since C doesn't have any syntax for objects). And there is also code for main loops so you can write event-driven programs.
For more info see wikipedia: https://en.wikipedia.org/wiki/GLib
Glib was originally part of GTK, but the non-GUI code has been completely factored out so that you can use it in command-line programs: http://library.gnome.org/devel/glib/stable/
CLIB
Adding another option (full disclosure, I am the author); if you can compile and link C++, you can have a look into libcdada, which has a pure C API, but uses libstdc++ as a backend for most of the containers:
https://github.com/msune/libcdada
Well since STL's very nature is based on templates which C doesn't have, it would be difficult to even come close to the STL in C. The best you could hope for is some collection classes which manipulate void* pointers to unknown object.