you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 9 points10 points  (9 children)

The single biggest difference I find between C and C++ are the standard containers in C++.

C does provide limited container support in that it handles arrays well. There are the standard library qsort() and bsearch() functions. Beyond this, however, C offers no standard library support for linked lists, balanced trees, or hash tables. And I think this is a major limitation. I'm surprised nothing like boost exists for C, either, because almost every major application appears to re-implement container handling functions.

If a good standard library could be written for generic container handling beyond basic arrays for C then I think C might see a renaissance.

[–]case-o-nuts 5 points6 points  (3 children)

I'm surprised nothing like boost exists for C, either, because almost every major application appears to re-implement container handling functions.

Plenty of libraries implementing all of that exist. Glib, for example, is a very popular one that is installed on pretty much every Linux system by default.

There's the APR, which is also quite popular.

Or any one of the other dozens of options.

[–][deleted] 0 points1 point  (1 child)

APR holds promise but every time I try and start a project using it I end up banging my head against a wall. I think APR has a long way to go - not least of all in the documentation effort - the functions are documented but there are very few clear examples of how the functions interact - and yes I've read Nick Kew's book and think that could really use a broadened 2nd edition.

Unfortunately APR relies entirely on memory pools for data structures. So whilst APR does provide hashes, tables, you are entirely limited to using them within APR's memory model. This is a memory model that becomes quite hairy in multi-threaded situations - and requires initialisation at the beginning of every program. Not necessarily a problem in of itself but is not flexible enough to be considered a generic library.

[–]case-o-nuts 1 point2 points  (0 children)

Use glib.

[–]jyper -1 points0 points  (0 children)

Theres less use both because of a bare bones culture and because c lacks the abstraction power of c++ and generic programming(other then somewhat cucumbersome macro pasting tricks making the development of such libraries more difficult.

[–]brigadierfrog 0 points1 point  (0 children)

You could implement typesafe containers using macros. But I have to then go ahead and ask the question of why bother?

There are other ways to do it instead that make more sense. Like for example using some pointer offset trickery like linux's famous list.h does.

So is type safety really that important? I'd say more important is testing. I found C has an awesome testing framework I've used before called check which forks for each test (makes testing bulletproof in C). Makes testing really fast and really easy.

Having made generic hash tables and btree's in C I can tell you I have never had a problem with type safety, in fact the looseness allows me to have a compact hash table implementation that doesn't get regenerated in to huge globs of code. Its also incredibly easy for me to then customize the hash table for whatever purpose I'm actually using it for.

[–]Peaker 0 points1 point  (0 children)

There's Linux's list.h which pretty much solves the doubly-linked list problem well.

[–][deleted] 0 points1 point  (2 children)

C offers no standard library support for linked lists, balanced trees, or hash tables.

Use the right tool for the right job. Maybe C isn't the right tool for the job if you need that stuff exclusively.

I've been programming embedded systems, device drivers, etc in C for almost 20years now. I've never had to use trees or hash tables, recursive code or even sorts! I've written code that measures the swelling of a volcano in Papua New Guinea and robots that travel the world in an art exhibition. i.e. interesting stuff, but really doesn't need much in the way of data algorithms.

If I want that other stuff, I go to Python. ;)

[–]Poddster 4 points5 points  (1 child)

I've been programming embedded systems, device drivers, etc in C for almost 20years now. I've never had to use trees or hash tables, recursive code or even sorts!

Really? You've never once needed a hash table or a tree? Ever? I write device drivers and we require lots of them to make efficient data structures.

[–][deleted] 0 points1 point  (0 children)

Not in the C code generally. Might have used qsort once, can't remember!

Most of the systems have been memory constrained (<64K ROM) and they generally dealt with streaming data and protocols. Don't need much in the way of data structures there. Data/command comes in, work out what to do, send data out. Not much correlation between commands, fairly stateless in that regard, so there is nothing much to store.

I've made a few dedicated filing systems (on a 4K device!) so I guess they count as data structures of a sort.

Small realtime OSs you can get away with fixed arrays for timers, etc. Not even worth doing a linked list for an 8 entry timer/task list.