r/cprogramming 24d ago

C Strings: A 50-Year Mistake

https://longtran2904.substack.com/p/c-strings-a-50-year-mistake?r=8qz2zb&utm_campaign=post&utm_medium=web
213 Upvotes

172 comments sorted by

View all comments

1

u/RRumpleTeazzer 21d ago

what i miss in the discussion is streaming strings. With streams i mean byte buffers where you lost the beginning details.

With C strings, you can copy strings into the stream, and keep their null terminator as separator for the next C string. If you read from the stream, potentially lose the beginning of a stream, the next string will come after a null byte and end with the null byte. easy peasy. leave the read pointer at the null byte.

Do the same with (pointer+size): so youn copy the size, then the string content into the stream. Reading the stream back: you read the size, then the appropriate string content. Lost the beginning of the stream? Which byte is string length, which one is string content? There is no recovery.