65,186
社区成员




#include <iostream>
#include <string>
#include <cstddef>
using namespace std;
int main()
{
int *pi = new int[12];
cout << "pi: " << sizeof(pi)
<<" *pi: " << sizeof(*pi)
<< endl;
string st1("foobar");
string st2("a mighty oak");
string *ps = &st1;
cout << "st1: " << sizeof(st1) //string的长度,不同的编译器内部实现不同,VC++6.0=16
<<" st2: " << sizeof(st2) //同上
<<" ps: " << sizeof(ps) //指针,在32位机中占4个字节
<<" *ps: " <<sizeof(*ps) //同string长度,不同编译器不同
<< endl;
cout << "short: \t" << sizeof(short) << endl;
cout << "short*: \t" << sizeof(short*) << endl;
cout << "short&: \t" << sizeof(short&) << endl;
cout << "short[3]: \t" << sizeof(short[3]) << endl;
}
cout << "short: \t" << sizeof(short) << endl;
cout << "short*: \t" << sizeof(short*) << endl;
cout << "short&: \t" << sizeof(short&) << endl;
cout << "short[3]: \t" << sizeof(short[3]) << endl;
cout << "short&: \t" << sizeof(short&) << endl;
为什么是2个字节呢?