115 points · by signa11 · 2y ago · 48 comments
another general point is that almost all the functions return void. this means that they must have side-effects. i am very far from being an FP maniac, but i do like a bit of purity when i can get it.
also, some comments would be nice.
bottom line - not for me, but then i haven't written any C for over 30 years :-)
Technically, you can implement that spec for all OSes. I did a few times, defining vendor-specific facilities for Linux components I used, like xwindows, drm/kms, etc..
of course, there are reasons for mucking around with pointers. but down that route lies pointers-to-pointers, and eventually madness.
What baffles me is the lack of any tests here. I understand writing tests is boring, and I understand writing tests in C is the least fun writing tests could be, but: I was thinking how neat this is and how I was going to use this for my own projects, until I saw it has no tests.
C is one of the few languages where, unless you're formally verifying it on paper, you need static analysers, dynamic analysers, and unit tests, all at the same time, to make sure your programs pass the bare minimum stability required to be used.
> Array: Implements a dynamic array similar to std::array in C++.
should read "static array". std::array requires you to state the size.
C++ std::arrays are pretty much just uniformly typed tuple structures, not dynamic arrays.
In fact you can use them directly on the stack with no memory allocations whatsoever.
The documentation is correct regarding what c_std has actually implemented. And it's still useful, unlike vector the size is (dynamically) set once at construction and then enforced.
int array[N];
is one.(Your example can go on the stack. A struct with a final array member of unspecified size can work as an abstract data structure. But you can't have both at once.)
I don't remember exactly what but I have run into it before, I think the iterator implementation is a raw pointer and that breaks the interface expectations a bit?
It's just a C framework, and not nearly as high quality as say, Glib.
[0] https://www.phoronix.com/news/CPP-Linux-Kernel-2024-Discuss
Vector* vector_create(size_t itemSize) {
Vector* vec = (Vector*)malloc(sizeof(Vector));
// ...
return vec;
}
This just immediately strike me as poor code.C is not java, you do not have to "new" up every little structure.
void vector_init(Vector *v, size_t itemSize) {
v->itemSize = itemSize;
// ...
}
Instantly cleaner, more efficient, less memory allocation/handling needed, lets the user control where the Vector is stored.Good luck with future development :)
Your library demonstrates that C++ is the superior language because it can to template specialization, resulting in better machine code. For example, your std::span implementation needs to store the element size, resulting in a larger structure (24 instead of 16 bytes), more memory memory accesses, costly integer multiplications everywhere.
C++ can omit all this, and can do simple bit shifts instead of multiplications.
There are many more places where you demonstrate C++'s superiority (e.g. it can safely do deep copies with no special code, while your library can't do copies at all, and even if it could, doing so safely/deeply would be extremely cumbersome with C, both for your library code AND for the code calling your library; all a piece of cake in C++).
Oh, and "Implements a dynamic array similar to std::array" .... that's factually wrong. std::array is not a dynamic array. std::vector is. Interestingly, your std::array implementation uses your std::vector, while adding some more runtime overhead. Hey, it's C, it's slower than C++ is what I learn again here!
It's a rather pointless library, unless your point is to demonstrate that C is a bad programming language.
There are lots of plain C container libraries which are probably suited better for C, if you really must use C, or prefer C for whatever reason that escapes my imagination.
And then rise of FOSS happened, with the original GNU contribution guidelines asserting only C and Lisp as the all mighty languages for the GNU ecosystem to build upon.
A good C library would use intrusive data structures, user-controlled memory allocations, not try to replace built-in types (arrays) with inferior alternatives, and not to try to be a kitchen-sink library.
And then it would be obvious such C++ sucks in comparison... ;-)
That's par for the course in c++.
> not try to replace built-in types (arrays)
How do you pass around C arrays to avoid the braindead pointer decay? You wrap them in a struct. That's literally all that std::array does.
Irrelevant. We need good lib in C