4 ms·
Which parts of the C standard library has any need for allocators?
by codr7 1y ago
Which parts of the C standard library has any need for allocators?
- gingerBill 1y agoLoads of libc allocate. The trivial ones being malloc/calloc/free/strdup/etc, but many other things within it will also allocate like qsort. And that means you cannot change how those things allocate either.
- uecker 1y agomalloc/calloc/free is the allocator, so it makes no sense to pass it an allocator to it. qsort does not allocate. I think strdup is the only other function that allocates and it is a fairly new convenience function that would not be as convenient if you had to pass an allocator.
- gingerBill 1y agoMany implementations of qsort do allocate using malloc. And I know malloc/free is the allocator, but you cannot override it either.
- uecker 1y agoCan you point me to a qsort that does call malloc? This is news to me as the API is designed to not require this. There is no standard way to overwrite malloc/free (which would be a limitation when using other libraries that do not make the allocator configurable), but it is often supported (e.g. malloc_hook in GNU libc) or can be done using the linker.
- broken_broken_ 1y agoGlibc’s one does and it caused a security vulnerability: https://www.qualys.com/2024/01/30/qsort.txt https://www.qualys.com/2024/01/30/qsort.txt TBH it was also news to me, I discovered it randomly while browsing vulnerabilities… Printf also allocates, and a ton of other stdlib functions as well.
- uecker 1y agoI now remember that I saw this before. But somehow I do not feel like it is a language problem when a standard library implementation allocates a temporary buffer using malloc under the hood as performance optimization.
- codr7 1y agoYeah that's my hunch as well, and replacing strdup if you absolutely have to isn't really a big problem.