by nine_k · 8y ago · 0 comments
This decision, if not the single most prolific, is likely one of the top 3 sources of security exploits in C code. All for the want of saving a few bytes.
Then they added 16-bit strings. And eventually 32-bit strings. Which are useless to keep that 5GB file in memory you process these days. I'm not up-to-date - does pascal have a 64-bit string type?
C, on the other hand, still uses the same error-prone and exploit inducing strcpy, strcat and friends - and the work for those 12GB gene texts and .csv files.
C never looked elegant, but it was extremely effective in the limited memory / limited CPU days of yore, and that's why it won against e.g. Pascal.
Seems like an easy fix is a single type of string with dynamic size depending on the first byte, like UTF-8:
0bbbbbbb : 1 byte header for a string up to 127 bytes long
10bbbbbb + hh : 2 byte header for a string up to 16384 bytes long
110bbbbb + hhhhhh: 4 byte header for a string up to 512 MB long
1110bbbb + hhhhhhhhhhhhhh: 8 byte header for a string up 1.15 EB long
... etc. (so as not to repeat the mistakes of Pascal, who knows how long will 1.15 EB be enough for everybody)
You would optimize the functions for the fast path with a single bit test for short <128 byte strings, and end up with a sane, safe string until the end of time. With minimal memory impact and much faster than testing for nul terminators, strlen() all over the place etc.
It's so puzzling that this is a data structure I can't recall ever seeing in the wild.
AT&T was forbidden to sell UNIX, so they provided the source code to universities at the symbolic price of $100, which was basically free when compared at how much something like VMS or z/OS would cost in licensing.
When Linux came into the scene, C was already on its way out, being slowly replaced by C++ on OS/2, Windows, Mac OS, Symbian, BeOS, NewtonOS as the way to go for writing applications. The later three were even written in C++ (Newton also had NewtonScript).
In 1994 we were already teaching C++ to first year CS students at my university. Proper C++, following Bjarne's ARM book, already talking about RAII and stronger type checking.
It was UNIX FOSS with the assertion that portable software had to be written in C that eventually pushed C everywhere, even to the platforms that were already getting cosy with C++.
More like AT&T and Unix monopoly?
Furthermore, using mmap (which was always the right way to do it), it doesn't matter if it's 512KB or 1GB or 5GB or 20GB, and is supported by C string practices but none of the Pascal strings (or those suggested on this thread).
Safe string manipulation in C devolves into always passing a char array and a count anyway, which is not coincidentally exactly what Pascal strings look like.
Can't blame people who worked on machines with 128 kilobytes of RAM for that. They likely did not expect that their well-meaning hacks will take over most of the computing world.
Still being sold today as Unisys ClearPath mainframes.
https://www.mikroe.com/compilers
Also many of those micro-controllers are more powerful than the computers where we ran CP/M on, where we had plenty of system languages to choose from.
They are have become overwhelmingly C due to synergy effects.