以下是在64位系统中C++各类型的sizeof大小的测试
#include<iostream>
#include<string>
using namespace std;
int main(){
char c,*pc;
bool b,*pb;
int i,*pi;
float f,*pf;
double d,*pd;
string s,*ps;
cout<<sizeof(c)<<" "<<sizeof(pc)<<' '<<sizeof(b)<<" "<<sizeof(pb)<<' '<<sizeof(i)<<" "<<sizeof(pi)<<' '\
<<sizeof(f)<<" "<<sizeof(pf)<<' '<<sizeof(d)<<" "<<sizeof(pd)<<' '<<sizeof(s)<<" "<<sizeof(ps)<<' ';
return 0;
}
结果:1 8 1 8 4 8 4 8 8 8 8 8
char , bool , int , float , double , string分别占1、1、4、4、8 、 8个字节;long的大小和编译器平台有关,long long是8个字节。
指针类型都是占8个字节。
#include<iostream>
#include<string>
using namespace std;
int main(){
char x[10] = "hello";
char y[] = "hello";
char *p = x;
char *q = y;
cout<<sizeof(x)<<" "<<sizeof(y)<<" "<<sizeof(p)<<" "<<sizeof(q)<<" "<<endl;
return 0;
}
结果:10 6 8 8