I have read following in The C++ Programming Language special 3rd edition that:
Whether two identical character literals are allocated as one is implementation defined (§C.1).
const char* p="Heraclitus";
const char* q="Heraclitus";
void g ()
{
if (p == q ) cout << "one!\n"; // result is implementation defined
// ...
}
Note that == compares addresses (pointer values) when applied to pointers, and not the values pointed to.
I have tried following program on gcc 4.8.1 & MSVS 2010
#include <iostream>
int main()
{
const char* p="Heraclitus";
const char* q="Heraclitus";
if(p==q)
std::cout<<"fine!!!";
else
std::cout<<"!fine";
}
Output:
fine!!! (on gcc 4.8.1)
!fine (on MSVS 2010)
Why this is left as implementation defined behavior? What is the reason?
We can find a rationale in this comp.std.c thread: History question: String literals and it says: