You call free(foo) not free(foo, 12) or free(foo, strlen(foo));
But there's no standard way to ask for this size. Why doesn't strcpy just check with the allocator about how much memory is available and refuse to write past that.
And if the memory allocator doesn't know about the string then you could just revert to the existing behaviour so you're no worse off.
static char buf[128];
and use strcpy() at any point to copy into it, so I don't think statically allocated strings must be literals.The point is not that allocators don't know how much memory has been allocated, but that C has no idea about the underlying allocator, so it cannot ask it about the length of the string as it was proposed.
How much C code is out there that only uses malloc/free? All of those programs could be made safer if the default string methods were able to prevent writing past the end of the array.
Would be nice to be able to tell if something is allocated on the heap or stack in a portable way.
As in being able to ask if and object is thread local or not. The language and run time could support that but studiously does not.
Just like the language could allow you to directly monkey with the ABI but does not.
I looked into that, not available on many many platforms.
I think the inclusion of strdup() shows that it was a mistake to make C strings completely allocator-agnostic.
That said, several “high-level” string libraries for C exist that make it safer, more efficient, etc. Some of them are even pretty clever, allocating extra space right before the C pointer to a null terminated C string that it gives you, so it can do its own bookkeeping.
It is not the case that C has a benevolent dictator with a particular vision and drive for the language. It's more like a small group of people who don't want to mess things up. And so not much changes. This is neither good nor bad to me--it simply is; and anyway, these days, you have quite a few options that do offer automatic strings that can also compile into machine code (Go, Rust I suppose?).
Getting rid of standard string functions would be the best thing to happen to the c language in the last 25 years.
There are two types of c programs. Those that scrupulously avoid standard string functions and brittle programs shot through with security holes.
In other words, if your pseudo-c candidate already suffers from most the interop problems that Rust, D, Nim and Go do, why not just use one of those and at least reap the other benefits they provide?
Your suggested dichotomy is, of course, a little bit false. But I'm sure you knew that when you wrote it down.
It's entirely possible to write secure programs in C, even with standard functions. Writing your own code does not somehow confer a level of security-consciousness that you lacked when sticking to strings.h. (It does give you a wonderful opportunity to write your own security holes that no one has discovered yet!)
I mentioned this somewhere else, but we're in a pretty good place right now with languages; we finally have really solid alternatives to C that can compile to machine code, in both Go and Rust.
You realize that most standard string functions are outright banned by organizations that care about security. As in you're not allowed to use them not even if you pinky swear to be 'careful'
Edit: Actually seems I confused Fortran and Pascal. Also, Fortran uses an internal length variable.
The reasons aren't technical or based on merit but on stubborn ideology.
But a plain *char in C makes no such distinction.