+ -
当前位置:首页 → 问答吧 → 疑问...

疑问...

时间:2011-12-06

来源:互联网

const char* ca[] = {
  "pralines & cream", 
  "fudge ripple",
  "wild mountain blackberry"
};
cout << sizeof(ca) << endl;
cout << sizeof(*ca) << endl;

这个很好理解,ca是一个存储指针的数据,而*ca是一个指针,所以 sizeof(*ca) = 4, sizeof(ca) = 12.
但是:
const string iceCream[] = {
  "pralines & cream",
  "fudge ripple",
  "wild mountain blackberry"
};
cout << sizeof(iceCream) << endl;
cout << sizeof(*iceCream) << endl;

iceCream是一个存储string的数组,string的构造器直接将C-style转化为string对象。*iceCream不是string对象么,那么为什么sizeof(*iceCream) = 4, sizeof(iceCream)=12 ????

PS:
sizeof不是在编译期间就计算出来的么?那么编译期间就知道结构体/类的内存如何对齐的吗????




作者: halonar   发布时间: 2011-12-06

cout << &iceCream[0] << endl;
  cout << &iceCream[1] << endl;
  cout << &iceCream[2] << endl;
我打印了地址,的确输出是:
0x22f2f4
0x22f2f8
0x22f2fc
说明果然:iceCream[0]只是占了4个字节,但是我想知道iceCream[0]不是string对象,它存在哪了?

作者: halonar   发布时间: 2011-12-06