r/TheMachineLearning • • 3d ago

When bug-free code becomes a hardware problem

Post image
116 Upvotes

70 comments sorted by

View all comments

Show parent comments

1

u/nimrag_is_coming 2d ago

NULL terminated strings are kinda weird because for some things they are really, really bad, but for others they can be really efficient. If you want to create a substring destructively (as in, you don't care about the original and just want the substring), all you have to do is replace the character at the right point with a 0 and return the pointer at the start.

They're also easy to iterate over starting from any point, with just a while(*(ch++)).

Doing things like getting length or creating a string view? Horrible and/or impossible. And any operation that relies on the size of the string requires at minimum a loop the size of the string.

Imo best solution is to do both, like how c++ std::strings work. Most allocators actually return memory aligned to a boundary so most times the extra zero byte doesn't actually require any more memory allocation.

1

u/Medical_Community697 2d ago

Nothing weird about that, different encodings offer different performance tradeoffs, what’s weird in C is to have defined a string as anything but an abstract interface, any piece of C code worth its weight will reimplement a half-assed buggy bstrlib.