11

Possible Duplicate:
Container Class / Library for C

One of the primary reasons to use C++ over C is the superbly convenient containers that STL provides. However, if I want to write my code in pure C and not have to write all my containers from scratch, what alternative do I have ?

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)

Any opinions and/or experiences with containers in pure C (Ansi or otherwise) would be much appreciated.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Sandeep
  • 1,745
  • 3
  • 20
  • 30
  • 1
    The real question is, why do you want to write in C? It is really not possible to produce robust container libraries in C, and C++ compilers are available for all but the smallest of platforms, so why not use C++? –  May 20 '11 at 17:02
  • thank you for the trolls. But I should take some of the blame myself - I should have simply asked about containers in C and left it at that. – Sandeep May 22 '11 at 04:57

2 Answers2

3

I'd recommend GLib solely because it's got a nice set of features and it's relatively mature, stable, portable, and widely used.

kqnr
  • 3,596
  • 19
  • 17
0

You, or a library writer, can write containers etc. each time you want to use them for a different type, possibly putting definitions into gigantic macros, or you can use void * for containers, losing all hope of type safety and sometimes some performance. (C's qsort function can be significantly less efficient than C++'s sort template.) There is no way to get the equivalent of C++ containers, iterators, and algorithms in C.

I don't know much about Glib, and your reference to disparate elements from Linux kernel headers is a little vague. The Linux list you mention is probably typical of what you'd get: no type safety and a set of well-written functions that will be named differently for each data type. A quick look at iMatix didn't disclose any containers.

David Thornley
  • 56,304
  • 9
  • 91
  • 158
  • 2
    What are you talking about? if you don't know Glib why do you answer? C++ compiler is written in C and you claim "There is no way to have C++ containers and etc in C? the only difference is syntax otherwise you can have everything in C, the Entire Unix OSes, BSD, Linux and etc are written in C – kay May 22 '11 at 09:23
  • 2
    @kay: The question of what you can write in C is irrelevant to the question of what language constructs you can build in C. If you mean that you can write a compiler for another language in C, and use that instead, you're missing the point. – David Thornley May 23 '11 at 14:07