Re:Imposing memory security in C [video]
by pizlonator on Thursday February 27, 2025 @03:09PM
(#43197932)
Yeah totally.
It's a perfect example of C being optimized for simple mapping onto linear memory, rather than some kind of performance optimum
Re:Imposing memory security in C [video]
by Dylan16807 on Friday February 28, 2025 @09:35AM
(#43206066)
I don't know, a pointer with a size is also quite simple.
C has another big speed problem. Strings are 0 terminated, rather than length terminated. This means constant scanning of strings to find their length. Even worse, the scanning of the string reloads the cache with the string contents, which is pretty bad for performance.
Of course, you could use `struct String { char *p; size_t length; };` but since every library you want to connect to uses 0 terminated strings, you're out on your island all alone, so pragmatically it does not work.
Another speed-destroying problem with C strings is you cannot take a substring that does not require allocating a new string and then copying the data. (Unless the substring is right-justified.) This is not fast in any universe.
D uses length-denoted strings as a basic data type, and with string processing code, it is much faster than C. Substrings are quick and easy. You can still interface with C because D string literals implicitly convert to C string literals, as the literals are 0 terminated. So this works in D:
(People sometimes rag on me for still using printf, but printf is the most optimized and debugged library function in the world, so I take advantage!)