5 ms·
Oh, I like this!! I might need to look deeper but how is memory management done? It seems like there’s some allocation happening on the background? For exampl
by _benj 3y ago
Oh, I like this!!
I might need to look deeper but how is memory management done?
It seems like there’s some allocation happening on the background? For example, the split() or string_to_json(), some memory is happening somewhere.
I ask for more than curiosity, I wonder if this could be used in embedded development.
Either way, seems like a fantastic toolkit for working in C!
I’m the most excited about the string functions, because idk why but I find myself doing a bunch of string manipulation quite often, and not having to jump out of C or manually implement algos sounds awesome!
- eesmith 3y ago> how is memory management done? It always uses malloc/free with no way to specify your own allocator, it nearly always assumes malloc succeeds, and when it does check it thinks malloc(0) returning NULL is an error, it prefers to malloc the maximal possible size then realloc smaller, and it does not document the API's memory ownership policy. (For example, you have to read the code to learn that sorted_strings returns a sorted copy of the input strings, rather than the sorted string array in-place.) A function like longest_common_prefix always mallocs space, when I would think just knowing the prefix size is more useful in an embedded program. > Either way, seems like a fantastic toolkit for working in C! Do take care as it appears to have a lot of small issues. Take https://github.com/gregoryc/standardlib/blob/4fb308a5716927e5622a0488d7aa104660c96841/standardlib.h#L4227 https://github.com/gregoryc/standardlib/blob/4fb308a5716927e... for example. It processes each regular file in a directory and saves the result to the file: char *file_data; char *new_str = map_function(file_data = read_file(entry->d_name)); free(file_data); write_file(entry->d_name, file_data); free(new_str); This should write new_str to the file, not file_data, which has already been freed. Going back to memory ownership, you can also see there is no documentation that map_function must return memory that will be freed by the caller. Digging deeper, "read_file" uses fopen(filename, "r") to open a file, then a SEEK_END/ftell() to get the size. There is a minor issue that ftell() may report -1 (on my machine it returns -1 for a fifo), causing a malloc(0) which is checked against NULL, and may falsely report a malloc failure rather than being an issue with how it tried to predict the size instead of using dynamic growth. (The places which use read_file first checks it's a real file, but the file can be changed between the check and its use.) I also think there's an issue with shellescape as it does not quote backslashes. I think the version in Python's shlex.quote() is more robust. Then there's the lack of documentation, like, do you expect startswith() and endswith() to be case-insensitive? I don't. Why would you use str_to_double() instead of atof(), and why is there no locale-independent variant? Quite a few of these seem like they should never be used, like "count_vowels()", which returns 0 for "sky" and 1 for "Brontë". And what is the difference between "is_string_alphanumeric" and "is_alphanumeric"? Quite a few of these functions might better be written using C11's _Generic (https://stackoverflow.com/questions/479207/how-to-achieve-function-overloading-in-c https://stackoverflow.com/questions/479207/how-to-achieve-fu... .
- _benj 3y agoThanks for the reply and analysis!