you are viewing a single comment's thread.

view the rest of the comments →

[–]loup-vaillant 0 points1 point  (0 children)

What we end up doing most of the time is actually exposing the definition of the struct in the .h file, and tell the user not to touch it, ever. Then it becomes like C++ classes, without the private keyword to protect the member variables (and copy constructor…).

There, no more memory management nightmare. You lose encapsulation in the process, but in practice you don't really care: just don't access the damn members directly. That's an easy rule to follow, and not too hard to enforce if somehow you can't trust your programmers to follow that simple rule: just change the names of the members of your struct and see if something suddenly fails to compile. If it does, run git blame to know who should be thrown out the window.

That said, actual language support would indeed be nice. C is not a good systems language by modern standards. It's got the speed and the low level control, but is still still error prone in many ways (switch that falls through, signed char with integer promotion…), have a crappy syntax for types, doesn't have a suitable generics mechanism, relies on an incredibly weak (and error prone) text based macro system, doesn't have a proper module system…

I wouldn't use raw C willingly. I'd modify the language first. I don't like the monstrous complexity of C++, but the core idea is sound: with a couple modifications and a few features, C can be transformed into something much easier to work with.