Live data from Hacker News

The Problem with C (2020)

cor3ntin.github.io

161–170 of 177 posts

Re: The Problem with C (2020)

#161

Earlier quoted context omitted.

> Zero terminated strings are only used in C so every other language needs to do a copy to pass a string to C, even C++ (for string_view at least). This is one of the most frequent complaints and I find it ridiculous. C has string literals that encode zero-terminated strings, but you don't have to rely on these zero terminators. There are a few awquard "string functions" in libc, most of which you should just ignore.…

> This is one of the most frequent complaints and I find it ridiculous. C has string literals that encode zero-terminated strings, but you don't have to rely on these zero terminators. Just a quick question. Is there somewhat up-to-date guide, list, book, whatever of good best practices for C, or maybe small libs for simple string handling, and common pitfalls (checking for over/underflows etc.)?

I don't know any in particular, but a few tips here.

I would say rule 1 is to not use strings unless needed :-)

My rule 2 would be to not use a library because that would probably add too much complexity (especially with regards to integrating memory allocation).

There are different valid ways to represent strings, but a basic approach is of course to use arrays of bytes, i.e. pointer + length (struct String { char buffer; int length; }). An important consideration is the choice of allocation scheme for the byte buffer. I'd recommend to use statically allocated strings (like char buffer[32];*) where possible, and to look into memory arenas. Don't make "resizable" strings unless absolutely needed (with resizeable strings you might run into dangling reference problems more easily, and you will probably need a "capacity" field in addition to pointer + length). Most dynamically-sized use cases do not need resizing; you can conveniently cover them with a separate string builder (which can be implemented using a large statically allocated storage or using a resized-as-needed storage. Once the string is assembled, the string builder can create the final immutable string by allocating for example from a memory arena.

Re: The Problem with C (2020)

#162
post #52

Earlier quoted context omitted.

The Zig creator is very conscious of this, talks a lot about rejecting many proposals and ideas to keep the language simple, not being an ass-hole about it, just to avoid what happened to C++.

And then the question is whether it will grow fast enough, or get overshadowed by a language that’s a bit more like a weed, growing fast in whatever direction it can, and whether it will then die because of lack of sunlight. It’s tricky to balance features, backward compatibility and ‘cleanliness’ in a (language, standard library)

You can come up with your own answer to that question by taking a look at how the Zig project is run.

https://kristoff.it/blog/interfacing-with-zig/ https://ziglang.org/zsf/ https://ziglang.org/news/financials-update/

Re: The Problem with C (2020)

#163
post #83

Earlier quoted context omitted.

We already had 20 years of hinsight that C's authors decided to ignore, had UNIX not been free beer for all practical purposes, history would have taken another path regarding C's adoption.

Yeah but the thought of living in a world where they use Ada++, AdaVM, and AdaScript makes me shudder...

It would have sufficed that the language had proper bounds checking, string and array types (that doesn't preclude pointers to everywhere on the universe), strong namespaced enumerations, required &array[0] instead of pointer decay, proper sized allocation.

All lessons from what was being done from JOVIAL, ESPOL, NEWP, PL/I various offsprings, during the 20 years that preceded C.

Re: The Problem with C (2020)

#164
post #147
post #84

Earlier quoted context omitted.

You might find the latest screwdriver tech at one of these ones, https://www.conexpoconagg.com/

Still not a screwdriver conference.

How can you tell, need to walk around the companies booths to see what construction tools they have on display.

Re: The Problem with C (2020)

#165
post #126

Earlier quoted context omitted.

It was when C++ came to be, 40 years ago. Don't mix stuff with how things are today.

Also, even now, the intersection between the two languages is large enough to be useful. I believe the entire Lua VM has been written in that subset, and so has my cryptographic library. Though you'd have to be careful, it's still possible today to write useful C code that also compiles as C++.

It surely is.

Re: The Problem with C (2020)

#166

Earlier quoted context omitted.

Well, one of the problems of C is that it's not as simple as it wants you to think it is. :-)

Right. You just move complexity into the codebase.

While true, most C coders are fine the idea of only introducing complexity upon demand.

No, what I mean is these subtle things that are impossible to get right: stupid integer promotion rules, needlessly complex declarations, messy stdlib functions, undefined behaviour as a feature, etc.

Re: The Problem with C (2020)

#167

Earlier quoted context omitted.

> C is the glue interface that connects all the different languages together. I wouldn't want to see anything new added to the language that complicates this lingua franca. Indeed, this is one of the two things where C is still king, the other being running on weird hardware with specialized compilers. That said, C is not a great glue language: - Zero terminated strings are only used in C so every other language need…

> Zero terminated strings are only used in C so every other language needs to do a copy to pass a string to C, even C++ (for string_view at least). This is one of the most frequent complaints and I find it ridiculous. C has string literals that encode zero-terminated strings, but you don't have to rely on these zero terminators. There are a few awquard "string functions" in libc, most of which you should just ignore.…

Because a C "string" is a char* by convention, any time a library exposes string parameters that's what they use. This includes libraries not written in C! I can't even think of a counter-example. Using an own string type adds too much friction, particularly because it prevents automatic binding generation. With errors such a standard doesn't even exist so you get a mess.

And yes, as a library developer you need to signal errors somehow, and whichever solution you use, the binding generator doesn't know about it. Rust has a nice error propagation mechanism which lets me attempt a set of operations but abort early when an error happens, but the binding generator can't use it.

Namespaces are a similar problem, they force library devs to try and come up with unique but short prefixes. Had C had namespaces the "short" constraint wouldn't be a problem, pretty much eliminating the chance of collision.

Am I right in assuming you're an embedded developer? Typically when I have this discussion with people the points that you make come from embedded devs. They're not reflective of the needs of library developers.

Re: The Problem with C (2020)

#168
post #64

Earlier quoted context omitted.

So just define your classes with struct. ¯\_(ツ)_/¯

But then all your privates will be public by default.

As I like to put my privates at the bottom of the definition of a class, having to use the class keyword and then having to put a public access specifier immediately after is fairly annoying :).

Re: The Problem with C (2020)

#169
post #44

C is just perfect as it is.

But K&R C was (aesthetically) more perfect. ANSI could not live with that, apparently.

You mean no function prototypes or just the idea that declarations defaulted to int?

Only used ANSI so wondering what excellence of K&R C mean

Re: The Problem with C (2020)

#170
post #98
post #39

Earlier quoted context omitted.

C cannot express quite a few useful things that assembly can. Things like tail calls, stack management, non-flat address space just cannot be expressed.

What's a non flat address space, and what would I use it for?

A segmented architecture is one example. Access to over 4gb of memory on 32 bit CPU is another.
Post reply on HN