《c++primer》中的例题,菜鸟的问题
关于sizeof的问题,在p132页中,有道例题:
#include<iostream>
#include<string>
#include<cstddef>
using namespace std;
int main()
{ int *pi=new int[12];
cout<<"*pi:"<<sizeof(*pi)<<endl;
cout<<"pi:"<<sizeof(pi)<<endl;
string st1("foobar");
string st2("a mighty ok");
string *ps=&st1;
cout<<"st1:"<<sizeof(st1)<<endl;
cout<<"st2:"<<sizeof(st2)<<endl;
cout<<"ps:"<<sizeof(ps)<<endl;
cout<<"*ps"<<sizeof(*ps)<<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;
}
我输出的结果是:st1和st2的sizeof都是4,而书上说是12?
怎么会这样?希望大家聊聊!