int main()
{
const char *p = "str";
string s = "str";
int a = 10;
cout << "str:" << (&"str") << endl;
printf( "p :0x%0x\n", p);
cout << "s :" << (&s) << endl;
cout << "a :" << (&a) << endl;
return 0;
}
str:0x4b8002
p :0x4b8002
s :0x79fdd0
a :0x79fdcc
可以看到,str这个常量字符串和const char*指的位置是一样的,都在内存的.rodata区。
而字符串s和局部变量a都是在栈上分配地址的,string并不存储在字符串常量区。