Live data from Hacker News

Common libraries and data structures for C

github.com

51–60 of 148 posts

Re: Common libraries and data structures for C

#51

What I find frustrating when I use C instead of C# is that I have to hunt for libraries and include them in the project or write my own implementation, even for most popular things like data structures, search algorithms, sorting algorithms, serialization, http calls. Whereas in C# the framework will provide them for me. If something is not in the standard library, I can use a directive or just reference a method fro…

>Go and Rust are similar in that aspect. I think this poses a subtle security risk about namespacing. Who authorizes these packages? Who audits these repositories? When you use a C/C++ library, there is obvious accountability. You know who maintains the repository (usually your distribution) or you explicitly copy someone elses code as a subrepository.

If you trust the compiler, why wouldn't you trust the standard library? They're usually made by the same people.

Re: Common libraries and data structures for C

#52
post #7

Good stuff. void* in itself is great for generics. C does support a templating system, like C++, with a little (reasonable) preprocessor abuse. It gives great cache contiguous results: https://www.github.com/glouw/ctl

I'm confused. It's easy to emulate a set using a map, but how do you emulate a map using a set?

Re: Common libraries and data structures for C

#53

As I got more into C programming, I started looking for data structure libraries. Found a few [0]. Also evaluated sc, but it had too much pre-processor magic for my taste. It also bundles random "stuff" like a URI parser, a thread abstraction, etc. Eventually I rolled my own [1] more focused library. It's basic and portable. 0: https://begriffs.com/posts/2020-08-31-portable-stable-softwa... 1: https://github.com/begr…

I may as well throw my hat into the ring: https://github.com/lelanthran/libds

I decided that I wanted to be able to simply drop a single .h file and a single .c file into any project without have to build a `libBlah.so` and link it to every project that needed (for example) a hashmap.

The practical result is that using the hashmap only requires me to copy the header and source files into the calling project.

It does build as a standalone library too, so you can link it if you want.

My primary reason for starting this is that I was pretty unsatisfied with all of the string libraries for C. When all I want to do is concatenate multiple strings together, I don't want to have to convert between `char *` and `struct stringtype *` everywhere.

The string functions are very useful as they all operate on the standard `char *` (nul-terminated) type.

Re: Common libraries and data structures for C

#54

For people advocating use of C++ instead of C, keep in mind there are several platforms (mostly embedded) that only support C and not C++. Also there are many projects that make use of C only. If C++ is available, I agree one should use it, however that is not always the choice.

Unless we are talking about something like Z80, 6502, PIC, AVR and similar low end CPUs, which hardly full support even C89, all modern CPUs have C++ compilers available.

More often than not, it is a matter of not wanting to use C++ than it not being available.

Re: Common libraries and data structures for C

#55
post #26
post #22

Earlier quoted context omitted.

C is supposely a language which it is reasonable to write a compiler for and in order to get a reasonable hardware ISA abstraction. Don't worry, the ISO working groups are making sure that it won't last and soon writting a C compiler will become a nightmare like what they did for c++ (C23 is seriously scary). Instead they should fix it: remove _Generic, typeof, etc which have nothing to do there, and make sure writti…

Keeping it simple to write a compiler is not a serious use case these days. People aren't bootstrapping new platforms from scratch. GCC and Clang are anything but simple.

Even if they aren't, people absolutely should be able to bootstrap new platforms from scratch. It's important to have confidence in our tools, in our ability to rebuild from scratch, and to be safe against the "trusting trust" attack among other things.

Lately I've been catching up on the state of the art in bootstrapping. Check out the live-bootstrap project. stage0 starts with a seed "compiler" of a couple hundred bytes that basically turns hex codes into bytes while stripping comments. A series of such text files per architecture work their way up to a full macro assembler, which is then used to write a mostly architecture-independent minimal C compiler, which then builds a larger compiler written in this subset of C. This then bootstraps a Scheme in which a full C compiler (mescc) is written, which then builds TinyCC, which then builds GCC 4, which works its way up to modern GCC for C++... It's a fascinating read:

https://github.com/oriansj/stage0

https://github.com/fosslinux/live-bootstrap/blob/master/part...

Even if no one is "using" this it should still be a primary motivator for keeping C simple.

Re: Common libraries and data structures for C

#56

For people advocating use of C++ instead of C, keep in mind there are several platforms (mostly embedded) that only support C and not C++. Also there are many projects that make use of C only. If C++ is available, I agree one should use it, however that is not always the choice.

Embedded devices really opened my eyes to this world. What a fool I was using malloc on my first Arduino project.

Arduino is entirely C++ though

Re: Common libraries and data structures for C

#57

For people advocating use of C++ instead of C, keep in mind there are several platforms (mostly embedded) that only support C and not C++. Also there are many projects that make use of C only. If C++ is available, I agree one should use it, however that is not always the choice.

> If C++ is available, I agree one should use it, C is very simple, while C++ is a monster with 100 heads, each speaking in another language. If all members of a project use the same subset of C++, then yes, it would be preferable over C. But if anyone or any team in a large project uses whatever he wants, things can get pretty complicated, pretty fast.

If the team doesn't care about code reviews, static analysis and automatic formating tools on PR merges, surely.

Re: Common libraries and data structures for C

#58

For people advocating use of C++ instead of C, keep in mind there are several platforms (mostly embedded) that only support C and not C++. Also there are many projects that make use of C only. If C++ is available, I agree one should use it, however that is not always the choice.

Somehow I enjoy writing C much more than C++

Re: Common libraries and data structures for C

#59

I work in C++ daily and there is something about the simple-ness of C that I love. You get out of magical hell that is templates and return to simple flat-functions and macros.

Nothing prevents you to code like that in C++, although I would rather suggest templates instead of macro hell.

Re: Common libraries and data structures for C

#60
post #36

I work in C++ daily and there is something about the simple-ness of C that I love. You get out of magical hell that is templates and return to simple flat-functions and macros.

Templates suck because they're the wrong abstraction. Or rather, they pretend to be an abstraction when in reality they're leaky as hell. Rust's traits are generics done right. I haven't looked at Concepts too much, I hope that they'll be better than templates.

They are, the best ISO C++ alternative is ISO C++ vnext.
Post reply on HN