Shao Miller said:
On 06/22/11 10:25 PM, James Kuyper wrote: [...]
The existing code problem was a acknowledged when C++ changed the type
of string literals. Compilers may choose not to issues a diagnostic for
this case. Now we have had over a decade to fix the smelly code, I
believe a diagnostic is now required by the new C++ standard.
C could and should have done the same, but as usual those worried about
breaking already broken code appear to have won the day.
Again, why should a C implementation be rendered non-conforming [to some
future Standard] thusly?
In a "bare metal" environment, one might very well wish to overwrite
their string literals' storage, no? The "bare metal" implementation
might need to define such action as being appropriate.
By using "proper" static arrays, we lose out on the "shared storage"
benefit. Writing for bare metal, hopefully one knows what one is doing.
Is this example silly?
If there's a need for a "bare metal" environment to be able to modify
string literals, that can be provided as an extension.
As in, a documented extension?
Any code that
currently takes advantage of that ability already has undefined
behavior.
Exactly the nature of my question. If there is at least one real
program that depends on this "undefined behaviour" (use the Standard
definition, please
), then that program's source code might have to
be adjusted for future versions of an implementation, depending on how
that future implementation implements the extension.
If such a feature were desirable, we could have an optional 'M' (for
modifiable) prefix for string literals, similar to the existing 'L'
prefix for wide string literals. For example, "hello" could be of type
const char[6], and M"hello" could be of type char[6]].
An interesting idea! 'M()' could get close, perhaps.
#define M(string) ((char[]){string})
I guess we'd need 'ML', too?
The fact that I've never heard of anyone implementing something like
this suggests (though not strongly) that there's no demand for it.
Suppose you've a program loaded into memory via a serial line. Suppose
the program is loaded into writable memory (that seems pretty likely).
Suppose the program offers a CLI. Suppose a user can rename commands or
variables, or redefine preset scripts. Yes, all of these could be done
cleanly (in my opinion) via 'static' 'char[]'s, but it can be more
convenient for some people to simply type the string literal right into
some spot in the source code where it's used and forget about coming up
with a meaningful (and possibly redundant) identifier, i.e.
'csz_Hello__world___And_how_are_you__today_'
I suspect that the vast majority of code that attempts to modify
string literals does so as the result of bugs.
That seems probable to me, too.
A lot more code
uses string literals in contexts that don't treat them as const,
but doesn't actually try to modify them; for example:
void func(char *s) {
printf("In func(), s = \"%s\"\n");
}
...
func("hello");
In short, I think the issue is not that anyone wants to modify
string literals;
For the right use case, I would.
it's that making them const would break existing
code that *doesn't* actually modify string literals.
(Stroustrup was able to do this in C++ because there was no existing
C++ code before he invented the language.)
Well doesn't 'const' "come from" C++, anyway?
And why isn't there a write-only counterpart, for symmetry, such as a
memory-mapped port that mustn't be read from?
And during very early development (and Usenet code examples), can it be
pleasant to avoid 'const' concerns altogether and then to analyze and
refine, gradually sprinkling 'const' in where appropriate? I don't
advocate this, but imagine that some folks might get "stuck" in
"analysis paralysis" if they had to think 'const'ness through at every
corner. I could be mistaken.