最近看到一些網上的sizeof的用法總結,但是自己試了試,發現好多都是錯的。
這裏重新總結一下,並有程序作證:
下面的程序都是在32位Windows系統下的測試:
#include<iostream>
using namespace std;
int main()
{
cout<<"char 佔字節數"<<sizeof(char)<<endl;
cout<<"short 佔字節數"<<sizeof(short)<<endl;
cout<<"int 佔字節數"<<sizeof(int)<<endl;
cout<<"long 佔字節數"<<sizeof(long)<<endl;
cout<<"long long 佔字節數"<<sizeof(long long)<<endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
void F1(char a[])
{
cout<<"F1 :"<<sizeof(a)<<endl;
}
void F2(char a[100]){
cout<<"F2 :"<<sizeof(a)<<endl;
}
void F3(char * a){
cout<<"F3 :"<<sizeof(a)<<endl;
}
int main()
{
char a[]="123456789";
char b[10];
char * c="123456789";
char * d=(char*)malloc(10);
cout<<"a :"<<sizeof(a)<<endl;
cout<<"b :"<<sizeof(b)<<endl;
cout<<"c :"<<sizeof(c)<<endl;
cout<<"d :"<<sizeof(d)<<endl;
////////////////////////////
F1(a);
F2(a);
F3(a);
system("pause");
return 0;
}