返回列表 發帖

[隋堂練習] Sizeof 練習運用

透過Sizeof函數運用
取得每一個型態所佔的記憶體大小
Ex: sizeof(int), sizeof(string)

並透過sizeof() 取得陣列大小

附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5.     int x[]={1,2,3,4,5,6,7,8,9,0};
  6.     cout<<"int:"<<sizeof(int)<<endl;
  7.     cout<<"string:"<<sizeof(string)<<endl;
  8.     cout<<"char:"<<sizeof(char)<<endl;
  9.     cout<<"float:"<<sizeof(float)<<endl;
  10.     cout<<"double:"<<sizeof(double)<<endl;
  11.     cout<<"陣列記憶體大小:"<<sizeof(x)<<endl;
  12.     cout<<"int:"<<sizeof(int)<<endl;
  13.     cout<<"陣列總長度"<<sizeof(x)/sizeof(int)<<endl;
  14.     for(int i=0;i<sizeof(x)/sizeof(int);i++){
  15.         cout<<x[i]<<",";        
  16.     }         
  17.     system("pause");
  18.     return 0;
  19. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {   
  6.      cout<<"Sizeof練習"<<endl;
  7.      cout<<"================="<<endl;
  8.      cout<<"int:\t"<<sizeof(int)<<endl;
  9.      cout<<"string:\t"<<sizeof(string)<<endl;
  10.      cout<<"char:\t"<<sizeof(char)<<endl;
  11.      cout<<"float:\t"<<sizeof(float)<<endl;
  12.      cout<<"double:\t"<<sizeof(double)<<endl;
  13.      int a[]={1,2,3,4,5,6,7,8,9,0};
  14.      cout<<"陣列記憶體大小:\t"<<sizeof(a)<<endl;
  15.      cout<<"int:\t"<<sizeof(int)<<endl;
  16.      cout<<"陣列總長度為:\t"<< sizeof(a) / sizeof(int) <<endl;
  17.      system("pause");
  18.      return 0;
  19. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a[]={1,2,3,4,5,6,7,8,9,0};
  7.     cout<<"int:"<<sizeof(int)<<endl;
  8.     cout<<"string:"<<sizeof(string)<<endl;
  9.     cout<<"char:"<<sizeof(char)<<endl;
  10.     cout<<"float:"<<sizeof(float)<<endl;
  11.     cout<<"double:"<<sizeof(int)<<endl;
  12.     cout<<"陣列記憶體大小:"<<sizeof(a)<<endl;
  13.     cout<<"int:"<<sizeof(int)<<endl;
  14.     cout<<"陣列總長度:"<<sizeof(a)/sizeof(int);
  15.     for(int i=0;i<sizeof(a)/sizeof(int);i++)
  16.     {
  17.         cout<<a[i]<<" ";
  18.     }
  19.     cout<<endl;
  20.     system("pause");
  21.     return 0;
  22. }
複製代碼

TOP

返回列表