News for hackers. Stuff that matters.
Re:Efficient string copying and concatenation in C by sdegutis on Thursday September 12, 2019 @03:07PM (#20955152)
I really love his trick of returning a pointer to the inside of his own allocated memory which is then compatible with C's stdlib string functions, while having his own string functions use pointer arithmetic to get the real struct back out and operate on that. Genius! This is the kind of thing that keeps drawing me to C every once in a while. C is almost like a puzzle, to figure out how you can accomplish what would be trivial in JavaScript, but with all these restrictions caused by an almost extreme simplicity. (Granted, C is not actually simple when you start hitting all the weird edge-cases and surprising UB, etc.)
Re:Efficient string copying and concatenation in C by stkdump on Thursday September 12, 2019 @03:31PM (#20955422)
I don't love this trick. To get this to work, they had to typedef the string type to char*, which means the API will also accept any normal string and probably crash at runtime. So to get this little convenience feature, type safety was sacrificed.
Re:Efficient string copying and concatenation in C by tmp0258 on Thursday September 12, 2019 @06:38PM (#20957216)
Seems to me that it would also prevent tools like the address sanitizer and valgrind from detecting some invalid memory accesses (corresponding to negative indexes). A "more conventional" string library [1] might be safer.

[1] for example: https://github.com/websnarf/bstrlib/blob/master/bstrlib.txt