|
|
|
|
|
by InclinedPlane
6025 days ago
|
|
I'd say using null-terminated strings rather than pascal style length embedded strings is C's biggest mistake. Responsible for so many inefficiencies (strlen is O(n) instead of O(1) as it should be) and, worse yet, so many incredibly serious security vulnerabilities. All to avoid having to incur a 1-3 byte per string overhead or figuring out how to efficiently work around a 255 character limit. |
|
If you want to turn some arbitrary data into a string, just put a 0 byte where you want it to end.
Instead of having to increment and compare both a counter and a pointer (or store a final pointer for comparison) in string-manipulation operations, you just increment the pointer. It makes for very concise loops in strchr, etc.
Tokenizing a string is just a matter of throwing down 0 bytes where the tokens are (as strtok does).
Passing a tailing subset of a string to a function is as easy as adding an integer to the string pointer, rather than requiring a memcpy and length calculation.