[tutorial] how to fix C/C++ INTERNAL string bug

Excuse me, but if I didn't know what I was talking about and my code was that bad, I think I would have posted in /r/shittyprogramming instead of Hacker News. And there's a gazillion of books that use "void main". The prevalence of an mistake does not make it any less of a mistake.

Anyway, while what you say may technically be true (I doubt it lmao), it comes from an academic ivory tower and completely ignores the lived experiences of actual C programmers and S code hackers. You doubt it. lmao. So sad. Why won't you read the standard and have it alleviate your doubts?

From ISO 9899:1999 (because that's what I have closer to me) §6.4.5p5-6:

The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence; for wide string literals... If the program attempts to modify such an array, the behavior is undefined.

Moreover §6.5.2.5p8 says:

String literals, and compound literals with const-qualified types, need not designate distinct objects.86)

And at the bottom of the page you have the secret meaning of this little "86)" hanging up there:

86) This allows implementations to share storage for string literals and constant compound literals with the same or overlapping representations.

What do you think a down-to-earth implementation - not some crazy academic ivory-tower bunch of standard committee know-it-all jerks - should do when you try to modify a value shared by others, who expect - as a matter of language guarantees - for it not to change? Please do tell!

Example 8 in §6.7.8 shows the difference between initializing a pointer and initializing an array with a string literal and says basically what the c-faq page does. But hey, who can trust them?

Everybody knows that. MSDN knows that. Even StackOverflow knows that. Of course cppreference knows this.

Everybody knows it sometimes (some might say "usually" or even "always from my experience", but I'm being careful here) happen. How is knowing that thing that almost always happens is "ignor[ing] the lived experiences of actual C programmers" is beyond my grasp.

/r/shittyprogramming Thread Parent