sizeof string

 

char a[] = "hello";
string s = "hello";
cout<<sizeof(a)<<endl;
cout<<sizeof(s)<<endl;
cout<<sizeof(s.c_str())<<endl;

  输出为

6
32
4

最后一个c_str返回的是char*,所有指针的长度都为4。sizeof(s)为什么为32?

查了一下,因为string是一个对象,这里相当于求一个对象的空间大小,具体内容应该是指针指向,动态分配。所以无论s有多少,返回的都是32.

 

posted @ 2014-06-06 16:09  Sara Chi  阅读(349)  评论(0编辑  收藏  举报