sizeof操作得出奇怪结论

image_trans image_trans 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在学<<C++ Primer>>时,遇到一个sizeof的问题,希望高手能给我解答一下。
下面是程序:
#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)
<<" st2: " << sizeof(st2)
<<" ps: " << sizeof(ps)
<<" *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:12 st2:12 ps:4 *ps:12

为什么我用g++编译后得到的相应项的结果却是:
st1: 4 st2: 4 ps: 4 *ps: 4

难不成是作者搞错了,我想应该不太可能吧。还是不同的编译器会有不同的结论?
...全文

相关推荐

评论 20
收藏 已收藏
分享
返回顶部
image_trans
C++ 语言 65,214 成员 250,514 内容