5 ms·
> that C makes it too hard to use a sorting library that someone competent has written. This makes no sense to me. What about C makes it hard to use a good lib
by JohnFen 4y ago
> that C makes it too hard to use a sorting library that someone competent has written.
This makes no sense to me. What about C makes it hard to use a good library?
- jjoonathan 4y agoC arrays suck on the best of days and qsort requires that you understand function pointers and types which are some of the hairiest C syntax in common use. The C Clockwise Spiral rule is truly special. It's easy to lose sight of the climb once you're at the top.
- JohnFen 4y agoWell, we could debate this, but it's all irrelevant to the assertion that C makes it hard to use good libraries.
- jjoonathan 4y agoOh, right, I forgot that C's library/package management situation sucks so hard that makes the awful syntax look like a comparatively small problem.
- JohnFen 4y agoThis actually made me laugh out loud. Yes, if your problem with C is that it doesn't need a package management mechanism like some other languages, then C is clearly not for you. But C is very far from the only language like this. It's a bit like criticizing a fish for having no legs.
- IshKebab 4y ago> if your problem with C is that it doesn't need a package management mechanism like some other languages The problem isn't that it doesn't need one, it's that it doesn't have one. I have no idea why you would think that it doesn't need one. Well there is vcpkg now anyway so it finally does have one.
- JohnFen 4y agoI sense that we have some sort of real miscommunication going on here, because the only response I can think of to > I have no idea why you would think that it doesn't need one. Is that I have no idea why anyone would think that it does need one. Perhaps the disconnect is that you are wishing C addresses different use cases than it addresses? That you wish it were a different language? If so, that's fine. Use a more appropriate language for your task. I just find it odd if the criticism of C is that it isn't a different kind of language.
- IshKebab 4y ago> Is that I have no idea why anyone would think that it does need one. For the same reason any language needs one. What is it about C that you think excludes it from the basic requirement of "using third party libraries"?
- cozzyd 4y agoC package management works great on my system. # dnf install foo-devel
- IshKebab 4y agoThat's suboptimal for many reasons. Package names are not consistent. Installing multiple versions is usually impossible. Huge pain for library authors. Doesn't integrate with build systems usually (even basic pkg-config support is iffy). The command is OS-specific. You can't usually choose the linking method. Difficult to bundle dependencies. Usually out of date. Etc. etc.
- cozzyd 4y agoit works quite well in practice, and unlike pip things don't break every 2 weeks.
- devnullbrain 4y agoI'd suspect kernel devs know about function pointers.
- jjoonathan 4y agoI'd expect kernel devs to carry lots of bad habits and poorly calibrated intuition from their noob days. Example: "for loops are fine."
- sgerenser 4y agoWhat’s wrong with for loops?
- jjoonathan 4y agoThey encourage accidentally quadratic behavior if they are easier than calling sort().
- asveikau 4y agoA lot of these comments are like "C is hard if you don't know C and it scares you". Not to mention kernel mode doesn't want a gigantic library package manager to pull in leftpad() from the internet. As mentioned, the kernel libraries on FreeBSD have a qsort, but they didn't in the original commit from 3 decades ago or whatever.
- me_again 4y agoI think it comes down to ecosystem fragmentation. A lot of languages now have common tooling (cargo for rust, pip for python, etc) which makes it easier to find and incorporate the libraries you want. Apparently there are tools like https://conan.io/ https://conan.io/ but they're not as widely-adopted. C's build system is similarly non-uniform. Many packages use Makefiles, others use different build mechanisms. C has no universally-agreed error handling mechanism or convention. Whether exceptions or golang's error interface, you can generally assume that a random package in most languages will handle errors the way you expect. In C it's a lot more varied. Similarly memory allocation - sometimes in a larger application you want to customize how malloc and friends work. How and whether you can do that for a C package is non-uniform. Mind you the C standard library has a sort() function which will have sensible big-O behavior on pretty much any platform. I suspect this specific problem is more to do with this being kernel-mode code which has a lot of special conditions.
- JohnFen 4y agoBut none of that supports the assertion that C makes it hard to use good libraries. You can even use libraries not written in C if you want. If the argument is really "it's impossible to make a good library in C", that's different. I'd very much disagree with that, but it would be to the point.
- me_again 4y agoI'm saying "it is harder to consume good libraries in C, because it is harder to find them & harder to build them; and once you have done both, you find that good library A and good library B work in very different ways, so you have to do more work to adapt". And I haven't mentioned the lack of a strong universal string type, the way many libraries typedef their own family of different kinds of integer, the way one library will require you to free() a returned structure and another will require you to call my_library_free()... It all adds up to additional friction. You don't have to agree! Maybe I am out of date, I haven't really dealt with this since the mid 2000's. I'd be thrilled to hear this isn't an issue any more.
- 4y ago
- zrm 4y ago> This makes no sense to me. What about C makes it hard to use a good library? It doesn't have templates/generics.
- deleted 4y ago[deleted]
- kevin_thibedeau 4y agoC11 added (limited) generic support
- remexre 4y agoYou can't specialize a generic algorithm for specific operations and data structures; in C++ terms, it gives you overloading, not templates.
- kevin_thibedeau 4y agoIt gives generic programming. C++ templates aren't the only possible implementation.
- kaba0 4y agoSo where is a generic vector data structure written in plain C that is efficient (that is, doesn’t store pointers to elements).
- zrm 4y agoThey used "_Generic" as a keyword but it doesn't really do that. Suppose I need to define a copy assignment operator for the library's sort function to use. Is there a good way to overload it? Can the library know what the size of each element is based on its type without having to pass it in as a parameter? You can pass function pointers to the library, but that quickly becomes awful. /* sort for basic integer types */ void lib_sort_u8(uint8_t *a, size_t count); void lib_sort_i8(int8_t *a, size_t count); void lib_sort_u16(uint16_t *a, size_t count); void lib_sort_i16(int16_t *a, size_t count); void lib_sort_u32(uint32_t *a, size_t count); void lib_sort_i32(int32_t *a, size_t count); void lib_sort_u64(uint64_t *a, size_t count); void lib_sort_i64(int64_t *a, size_t count); /* specify a comparator */ void lib_sort_comp(void *start, size_t nmemb, size_t size, int (*compar), const void *, const void *)); /* specify a comparator and an assignment operator */ void lib_sort_assign_comp(void *start, size_t nmemb, size_t size, void (*assign)(void *, const void *), int (*compar)(const void *, const void *)); /* specify a comparator, an assignment operator, and ... */ Or, you get one function that takes all of the arguments and have to define and pass in a bunch of function pointers and type size parameters that are each an opportunity for bugs or UB in order to sort a simple array of integers. If my type needs a custom assignment operator, I need each library I use to take that as an argument. One expects the function pointer to take the arguments in the order (src, dst), another as (dst, src), a third specifies the return value as int instead of void, a fourth takes the source argument as "void *" instead of "const void *" in case you want to implement move semantics and a fifth doesn't support custom assignment operators at all. It's no surprise that people prefer to avoid this.
- IshKebab 4y agoThere's no standard build system. Think about how you add a dependency in Rust, Go, JavaScript, even Python. Now do it in C/C++. Absolute nightmare. Look at how many header-only C++ libraries there are out there. They're making compilation time drastically worse purely to avoid the need to faff with a shitty build system. It's a good trade-off too. I use header-only libraries where possible. They're convenient and popular. Actually vcpkg seems to be going some way to fixing that but I have yet to convince my co-workers to use it.
- JohnFen 4y ago> to avoid the need to faff with a shitty build system. Then maybe don't use a shitty build system? It's true, C is not trying to be a programming environment or tech stack. It's a language, that's it. Whether or not that's desirable depends on what you're trying to do, it's not something that is good or bad in some absolute sense. You have your choice of build systems, so pick one that meets your needs. Vcpkg isn't for me, either, because it doesn't solve any problem I have. If it does for you, awesome!
- IshKebab 4y ago> You have your choice of build systems, so pick one that meets your needs. And can I pick the one that my dependencies use too? Didn't think so.
- JohnFen 4y agoYour dependencies have already been built. You're just linking to them. So yes, you can.
- inferiorhuman 4y agoAnd what if the dependencies haven't been built yet?
- icedchai 4y ago
- kaba0 4y agoC can’t be used to implement a good library for many problems due to being inexpressive. For example, you can’t write an efficient, generic vector data structure, but sort functions are also only fast due to the compiler’s smartness — passing a function pointer would be strictly slower. Though this has not much relevance here as it is about assembly.