5 ms·
Storing the length of a string alongside the string is a viable option in C, it's up to you to do it yourself though (whereas Java and almost every other langua
by abmackenzie 7y ago
Storing the length of a string alongside the string is a viable option in C, it's up to you to do it yourself though (whereas Java and almost every other language does it for you). See https://github.com/phusion/nginx/blob/master/src/core/ngx_string.h#L16 https://github.com/phusion/nginx/blob/master/src/core/ngx_st..., used by NGINX.
- tyingq 7y agoAlso sds, used by redis: https://github.com/antirez/sds https://github.com/antirez/sds
- pjmlp 7y agoWhich breaks down every time a C API needs to be called, and that linked API still has plenty of functions with separate pointer and length parameters.
- badsectoracula 7y agoYou can do it like in Free Pascal/Delphi: store the string both length-prefixed (for fast length access and bounds checking) and zero terminated (for passing to functions that expect zero terminated strings).
- pjmlp 7y agoAnd then the callee corrupts it.
- badsectoracula 7y agoIf the callee is meant to corrupt it then it is your fault for misusing it (unless the corruption is intentional and you plan to recalculate the length after calling it). If it wasn't meant to corrupt it then it is a bug and if it is in your code then it is your fault for not using the string functionality that you already have in place to avoid the bug in the first place. If it is not in your code, as long as you had to use it you'd have that bug regardless of what language or framework you used since it is out of your control and there isn't anything you can do about it.
- pjmlp 7y agoAka, C's community version of "you are holding it wrong".
- badsectoracula 7y agoIs there a language that doesn't allow any abuse of an API, including APIs that were not written in that language?
- pjmlp 7y agoYes, any system languages that doesn't need to depend on the existence of C. If you are going to mention Assembly as possibility, check ClearPath where there is no Assembly, NEWP has full control over the hardware stack.
- coldtea 7y ago>Storing the length of a string alongside the string is a viable option in C, it's up to you to do it yourself though Obviously, since many other languages that do so are implemented in C.