Subject: Re: Public Service Announcement: Please use /GF with Visual Studio when you want to collapse constant strings!
Lawrence D?Oliveiro <
ldo@nz.invalid> writes:
On Tue, 11 Aug 2026 15:10:13 -0000 (UTC), Richard wrote:
ChatGPT isn't that stupid and is more likely to alert you that you
are doing a meaningless address compare than an actual string
compare.
There is this thing called ?interning? of strings, which is precisely
to allow comparison of object addresses to substitute for comparison
of the actual object contents.
Java allows for this, for example. No reason why it couldn?t be
implemented in C as well.
There are plenty of reasons why it can't be implemented in C, at least
for all strings.
There are existing string interning libraries in C, but they can't
work on C strings in general. Such libraries can be useful, but
They can only work on strings controlled by the library itself.
#include <stdio.h>
int main(void) {
const char *lit = "hello";
char greeting[] = "hello";
if (greeting == lit) {
puts("Wrong");
}
greeting[0] = 'H';
if (greeting == lit) {
puts("Even more wrong");
}
}
Even `"hello" == "hello"` is unsafe (though apparently some compilers
have options that can make it "work").
This thread started because a lying troll claimed that it's a good idea
to use "==" for C strings.
--
Keith Thompson (The_Other_Keith)
Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
--- PyGate Linux v1.5.19
* Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)