Here is the code example(compiled and run in vs2015):
#include<cassert>
using namespace std;
int main() {
const char*p = "ohoh";
const char*p1 = "ohoh";
char p3[] = "ohoh";
char p4[] = "ohoh";
assert(p == p1);//OK,success,is this always true?
assert(p3 == p4);//failed
return 0;
}
As far as I know,the string literals are stored in the readonly
segment in address space,and const char*p = "ohoh";
just generate a pointer to that position.However,it seems like the compiler will just generate one copy of that string literal,so the p==p1
is true.
Is it a optimization ,or something guaranteed by the standard?