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.
Could you recommend a good general purpose data container library for C (pure C, not C++)?
I just need basic structures, like dynamic arrays, hash tables, etc.
(Note: By "good" I mean fast + elegant interface).
Isn't Glib sufficient for your needs ? :-)
developer.gnome.org/glib
I think Dave Hanson's C Interfaces and Implementations qualifies as both fast and elegant. It includes many different kinds of containers, some string processing, multiprecision arithmetic, exceptions, a couple of different memory managers. Nice stuff.
There's a book, but you don't need to buy it to use the software.
Apache Portable Runtime
http://apr.apache.org/
Includes modules for these areas.
Platform Definitions
Internal Memory Allocation
Atomic Operations
Dynamic Object Handling
Functions for manipulating the environment
Error Codes
File Information
File I/O Handling Functions
Filename Matching Functions
Miscellaneous library routines
Command Argument Parsing
Global Locking Routines
Hash Tables
General Purpose Library Routines
MMAP (Memory Map) Routines
Network Routines
Poll Routines
Memory Pool Functions
Portability Routines
Process Locking Routines
Random Functions
Ring Macro Implementations
Shared Memory Routines
Signal Handling
String routines
Internal APR support functions
Table and Array Functions
Condition Variable Routines
Thread Mutex Routines
Threads and Process Functions
Reader/Writer Lock Routines
Time Routines
User and Group ID Services
I previously recommended MemSL, but the website and company seem to have gone extinct. I would now suggest glib, it covers much of the same functionality and is widely supported and used throughout the Linux community.
I used MemSL when implementing an x86 kernel, and I found it to be general, reliable, and bug free.
From the description:
C Overview of the Memory Structures Library (MemSL)
The Memory Structures Library, MemSL for short, is a library of useful routines allowing the efficient use of complex data structures in C. The MemSL contains routines for managing:
Multi-Dimensional Dynamically Allocated Arrays
Single Linked Lists
Double Linked Lists
Circular Linked Lists
Cut, Copy and Paste with Linked Lists
Multiple Positional Pointers to Linked Lists
Stacks
Queues
Dequeues
Sets
Bags
Tables
Dictionaries
Hash Tables with Separate Chaining
Hash Tables with User-Defined Paging
Hash Tables with Dynamic Paging
Binary Search Trees
Threaded Binary Search Trees
AVL Balanced Binary Search Trees
AVL Balanced Threaded Binary Search Trees
Priority Heaps
Fully Dynamic Priority Search Queues
http://home.gna.org/gdsl/
You might also want to check out the iMatix SFL, to quote their webpage:
The SFL (Standard Function Library) from iMatix is a portable function
library for C/C++ programs. The SFL is the result of many years' development,
and is provided as Open Source software for the benefit of the
Internet community.
The SFL is written in ANSI C and has been ported to MS-DOS, Windows, OS/2, Linux
and other UNIX systems (IBM AIX, SunOS, HP/UX, Solaris, NetBSD, FreeBSD,
SCO OpenServer,> Digital UNIX) and Digital OpenVMS. It comes with complete sources and
documentation in HTML.
The SFL provides about 450 functions that cover these areas:
Compression, encryption, and encoding;
Datatype conversion and formatting;
Dates, times, and calendars;
Directory and environment access;
User and process groups;
Inverted bitmap indices;
Symbol tables;
Error message files;
Configuration files;
String manipulation and searching;
File access;
Internet socket access;
Internet programming (MIME, CGI);
SMTP (e-mail) access;
Server (batch) programming;
Program tracing.
The SFL is free software that you may use and distribute for private or commercial
purposes according to the SFL License Agreement.
More details about the API itself, see here.
Sglib "is a generic library for C that was inspired by the Standard Template Library from C++"
Let me add:
http://libslack.org
(... and add to this: libcx)
You may interested in looking at qLibc.
http://en.wikipedia.org/wiki/Qlibc
http://www.qdecoder.org/qlibc/
It's a general purpose complete C/C++ library which includes all kinds of containers and general library routines.
Related
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 10 years ago.
Member functions can be emulated in C by passing the this pointer explicitly. Virtual functions can be emulated by explicitly storing in every object a pointer to a global array of function pointers. Fine.
Now my question is, do people actually do this? I am wondering if it's worth teaching this technique, because I do not want to teach something to C freshmen that is practically never used in the real world.
(I need to fill the last day of a two-week introductory C course for people already familiar with OOP.)
Are there any relevant projects, libraries or frameworks that emulate OO in C in the manner described?
I've about twenty years experience in C. It was the first compiled language I learned and I've never needed to move on, so it's been C and only C, all the way. I write code constantly at work and at home. I have published a library of lock-free data structures. I think I'm a competent C programmer.
With regard to your question, OO consists of a number of concepts. One, for example, is instantiation, e.g a library with a new() and delete() and instances of a given entity (stack, list, etc). C supports this and it is, of course, a very functional and useful approach. I've used this approach for about fifteen years.
Many years ago I began experimenting with another OO concept, well supported in C++, inheritance. I wanted an entity which contained other entities. The problem then is exposing the API of the contained entites. You can do it, but the fact is, the C language does not naturally express such an concept and approach. It is not something I now use.
My advice is; a knife is a knife, a fork is a fork. You can use either as the other, but it doesn't work well. C does not naturally support some (important) OO concepts, such as inheritance. Don't try to make C do these things. If you want to do this, use C++.
Yes, they do.
Are there any relevant projects, libraries or frameworks that emulate OO in C in the manner described?
I wouldn't call it "emulating" just because there's no first-class language support. See GObject.
A lot of project uses the Object oriented paradigms in C codebase. For various reasons they don't use CPP directly. For system level or performance intensive projects, Other languages don't cut the deal. So its a battle between cpp and c.
Why people emulate OO in C instead of full blown CPP is topic of heated arguments. Linus torvalds once famously stated, CPP compilers are not trustworthy. He has little faith on CPP generated code.
Linux kernel is a good example of implementing OO design patterns in C. You can read about how Linux kernel did it in this lwn.net article series :
part1
part2
There is a extensive free document lying around in internet which covers a full range implementation OO design patterns in C.
ooc.pdf
You can find many other projects along the same road.
Examples:
pjsip
sofia
It may not be used in practice, but it is incredibly valuable to learn the concept of the equivalence between member functions and functions that take the object as the first parameter. Having this concept in the back of their head will help them in many problems they will encounter down the road.
Day in and day out I see people asking questions on Stack Overflow about why it doesn't work to point to pass a member function to something requiring function pointer, and things like that. They think that member functions are just some magical functions that are part of an object, and over-complicate the whole situation. If they had realized that member functions were equivalent to functions that took the object as the first parameter, then the problem they're having (that to call the method they would somehow need both the member function pointer as well as the object), as well as possible solutions (somehow pass the object in separately, or make some kind of closure that captures the object) becomes apparent. Apparently, too many people just pretend that OO is "magic" and don't understand this.
In functional programming, we often teach people how data structures and local variables and all that stuff could be written purely in terms of manipulation of functions. Not that this is practical -- it would probably be inefficient -- but this impresses upon them something about the power of functions. And it helps them to understand things in a different way. And maybe down the road if they write a compiler or something, these equivalences will come in handy.
Computer science is all about equivalences and reductions, and how to think about one problem in terms of another. We reduce SAT-3 to subset sum, not because that's actually how we would actually solve the SAT-3 problem, but because this teaches us that subset sum is NP-complete.
Every once in a while, I come across a piece of code written by someone else, where non-instance methods take a pointer to a structure as an argument, and I see a pattern and a light bulb goes off in my head, and I say, ah-ha, this can be re-factored into an instance method, because I know about this equivalence. So you see, knowing these equivalences also helps us to write better, simpler code.
Check out TI's "DSP Algorithm Standard" / xDAIS framework.
There's a generic C API that every conforming DSP algorithm implementation implements (sorry for the tautology). The need for all this "art" stems from several issues common in the DSP world:
relatively small RAMs
multiple data channels (often parallel/concurrent)
complex algorithm usage patterns
something else I forget
The standard and framework aim at making it easier for DSP engineers to use 3rd party DSP algorithms.
There's an interface to configure an algorithm instance and query its memory requirements (based on the configuration) and there are support functions that actually manage the memory.
Some memory areas, scratchpads, can be allocated temporarily and given to an algorithm instance when it's active and taken away from it when it's inactive and given to another instance, effectively shared.
There's also functionality (and APIs) to move instance memory buffers to defragment memory.
There's more, but I'd need to reread the docs to recall the details.
See IALG_*() and ALG_*() interface methods for example.
Also, there are tools to validate implementations of the generic APIs. 3rd parties can request official validation of them from TI.
Some relevant links: spru352g.pdf, spru360e.pdf.
I'm looking for a tool that can statically discover invariants in C programs. I checked out Daikon but it discovers invariants only dynamically.
Is there a tool available for what I'm looking for? Thanks!
See The SLAM project: debugging system software via static analysis. It claims to infer invariants statically, for just what you asked for, the C language. The author, Tom Ball, is widely known for stellar work in program analysis.
If you mean "invariant" in the widest sense, as the linked page to Daikon is using, then the work of many static analysis tools can be described as "discovering invariants", just perhaps not the expressive invariants you were looking for.
Frama-C's value analysis accumulates its results, the possible values of all variables, for each statement. At the end of the analysis, it can thus present non-relational information about the domain variation of each variable in the program, at each statement. In this screenshot, an invariant is that S is always 0, 1, 3 or 6 just before the selected instruction, for all executions of this deterministic program.
The two hidden parameters in your question are the shape of the invariants you are interested in, and the shape of the programs you want to find these invariants for. For instance, SLAM, mentioned in Ira's answer, was designed to work on device driver code, and to infer invariants that just contain the necessary information for verifying proper use of system APIs. Another tool, Astrée, is famous for doing a very good job at inferring just the right invariants to demonstrate runtime safety of flight control software.
The two degrees of freedom make for a very large design space. You won't find anything that works for all kinds of C programs and infers all the invariants you might be interested in, but if you refine your question for specific application domains and kinds of invariants, you will have better chances to find relevant answers.
Is there a library that implements lock-free algorithms(queue, linked list and others) written in C (not in C++)? I've taken a look at some libraries like Intel's, but I would like to use generic libraries, at least more generic than Intel's one.
See Practical lock-free data structures from the University of Cambridge
I've written my own, Rig, currently queue, stack and list are there, hash-table will soon follow. While I'm still working on it, it is intended for public consumption, and the API is mostly stable, just use the SVN trunk. :)
The only other such library in C that I know of is liblfds, though I've never used it.
liblfds
http://www.liblfds.org
Wiki with full API documentation, forum for questions, blog for reading the author rattle on :-)
Platform independent. Out of the box for Windows, Linux, Intel and ARM.
Release 7 should be out in a month or two. Will add run-time cache line alignment, backoff and SMR. (SMR also gives a ton of the other CPU types - basically, anything GCC compiles on which supports atomic ops, e.g. SPARC, MIPS, IA64, etc).
Also, there's no license - you can use the code however you want. Make money! It's not GPL.
I'm currently writing a lock-free lib but it's C++. Here's an STL-like Lock-Free Doubly-Linked List.
The memory manager it uses is quite powerful (32-bit CAS without ABA issues) so I'm using it to create a complete set of containers: a lock-free map/set (using skip lists), a lock-free bag (instead of queue/stack), and a lock-free unordered map (hash table using split-ordered lists).
For more info about the doubly-linked list check out my answer to a related question.
I'm looking for some C library that includes STM-style (Software Transactional Memory) hash maps, but I had no luck so far. It would be great if it was based on glib / gobject, but it's not that crucial. It also doesn't need proper transactions over many objects - single immutable hash support is all I really need.
Must haves: immutable snapshot read, lock-free write with auto-retry.
Wikipedia has a list of various STM implementations.
Well, I think (and there are a number of study) that current STM isn't faster than lock-free and mutex-based code. It is obvious: STM requires on-line data conflict checking. However, such conflict checking in pure software requires very huge time overheads. Currently, only Sun's ROCK processor supports a limited form of STM (Best-effort HTM with STM) by hardware. No x86 CPUs currently support TM in hardware. In short, STM is just slow.
In my opinion, you'd better just using a concurrent hash table. For example, you can find concurrent_hash_map in Intel TBB. Here is the link of TBB Manual. Oh, but it's C++, not C. But, I do believe you can (though it might take significant work) translate C++-based such hash table to C code. Intel TBB is open source.
Also, I think such highly concurrent (usually implemented as lock-free) data structures are not always useful. In some workload pattern, using such data structures is not good. To be sure, I recommend you to write a small micro-benchmark for two versions of hash tables: (1) lock-free and (2) lock-based. Also, please keep in mind the workload patterns for such micro-benchmark should be close to real one. An example could be found in here.
TBoost.STM has implemented a hash map in its example.
I am using C NOT C++!
I know the C++ collections, but I was wondering if Microsoft has a C based List structure of some type, like the linux kernel provides, that I can use in a user mode project?
I would prefer not rolling my own.
Only thing in the Windows API is interlocked singly linked lists, which are used via InterlockedPushEntrySList and InterlockedPopEntrySList .
For device drivers, there is LIST_ENTRY, but I am not sure if this can be pulled into user-mode.
Many algorithms books and websites contain implementations of linked lists that can easily be ported to C. Rolling your own is not too difficult.
reusable collections are tough in C, it just doesn't have the flexibility or metadata (how do you know when you are overflowing this array list and need to reallocate? How do will reallocate work if the rest of the code is using a custom alloc?
You can do it (you CAN do anything in c), but it gets abstract really fast.
On the other hand, creating a linked list in c yourself is downright fun. Arrays are already there, hashes are annoying but not impossible, trees are fun, ...
Also--people who think in c tend to be constantly optimizing. Setting every linked list operation behind a function call instead of just using this=this.next would probably disgust many of them (rightfully so).