r/C_Programming • • Sep 07 '26

Article I Made a Simple String Library

I wrote an article about this as well. Here it is.

There I explain why I do not like NUL-terminated strings and how I implemented my own simple string library in C.

If you have some spare time I would really appreciate some feedback on the article and the library.

The code is sitting on a codeberg repository.

Also, tell me what you think about C-style strings. Do you like them? Do you use them, or do you also tend to roll your own pointer + length structs?

9 Upvotes

31 comments sorted by

View all comments

Show parent comment

2

u/StrikeTechnical9429 Sep 08 '26

Such function have to allocate memory to create a copy of the string with NULL added. It isn't a problem, the problem is - who's supposed to free this memory? Anyway it wouldn't be possible to write something like

clib_function(convert_str_view_to_cstr(my_str_view));

because in this case one lose the returned value of convert_str_view_to_cstr() and wouldn't be able to use it to free memory.

3

u/Dieriba Sep 08 '26

Then store the pointer and freed it after, I mean this a drawback but that’s what happen when you make such lib you can’t have all you want, you must accept that there’ll be some limitation like this one.