源代码:
1 #include <iostream> 2 using namespace std; 3 4 #define SIZEOF(x) cout<<"sizeof("<<#x<<"):"<<sizeof(x)<<endl 5 6 struct EMPTY_STRUCT 7 { 8 }; 9 10 class EMPTY_CLASS 11 { 12 }; 13 14 int main() { 15 SIZEOF(bool); 16 SIZEOF(char); 17 SIZEOF(unsigned char); 18 SIZEOF(short); 19 SIZEOF(unsigned short); 20 SIZEOF(int); 21 SIZEOF(unsigned int); 22 SIZEOF(long); 23 SIZEOF(unsigned long); 24 SIZEOF(long long); 25 SIZEOF(unsigned long long); 26 SIZEOF(float); 27 SIZEOF(double); 28 29 SIZEOF(bool*); 30 SIZEOF(char*); 31 SIZEOF(unsigned char*); 32 SIZEOF(short*); 33 SIZEOF(unsigned short*); 34 SIZEOF(int*); 35 SIZEOF(unsigned int*); 36 SIZEOF(long*); 37 SIZEOF(unsigned long*); 38 SIZEOF(long long*); 39 SIZEOF(unsigned long long*); 40 SIZEOF(float*); 41 SIZEOF(double*); 42 43 SIZEOF(0); 44 SIZEOF(0.0f); 45 SIZEOF(0L); 46 SIZEOF(0LL); 47 SIZEOF(123); 48 SIZEOF(123456); 49 SIZEOF(1234567890000000); 50 SIZEOF(1<<7); 51 SIZEOF(1<<15); 52 SIZEOF(1<<31); 53 SIZEOF(1<<63); 54 SIZEOF(1L<<7); 55 SIZEOF(1L<<15); 56 SIZEOF(1L<<31); 57 SIZEOF(1L<<63); 58 SIZEOF(1LL<<7); 59 SIZEOF(1LL<<15); 60 SIZEOF(1LL<<31); 61 SIZEOF(1LL<<63); 62 SIZEOF("hello"); 63 SIZEOF("hello\\"); 64 65 SIZEOF(EMPTY_STRUCT); 66 SIZEOF(EMPTY_CLASS); 67 68 char str1[] = "china"; 69 char str2[10] = "china"; 70 char* str3 = str1; 71 char* str4 = str2; 72 SIZEOF(str1); 73 SIZEOF(str2); 74 SIZEOF(str3); 75 SIZEOF(str4); 76 77 return 0; 78 }
输出结果:
1 sizeof(bool):1 2 sizeof(char):1 3 sizeof(unsigned char):1 4 sizeof(short):2 5 sizeof(unsigned short):2 6 sizeof(int):4 7 sizeof(unsigned int):4 8 sizeof(long):4 9 sizeof(unsigned long):4 10 sizeof(long long):8 11 sizeof(unsigned long long):8 12 sizeof(float):4 13 sizeof(double):8 14 sizeof(bool*):4 15 sizeof(char*):4 16 sizeof(unsigned char*):4 17 sizeof(short*):4 18 sizeof(unsigned short*):4 19 sizeof(int*):4 20 sizeof(unsigned int*):4 21 sizeof(long*):4 22 sizeof(unsigned long*):4 23 sizeof(long long*):4 24 sizeof(unsigned long long*):4 25 sizeof(float*):4 26 sizeof(double*):4 27 sizeof(0):4 28 sizeof(0.0f):4 29 sizeof(0L):4 30 sizeof(0LL):8 31 sizeof(123):4 32 sizeof(123456):4 33 sizeof(1234567890000000):8 34 sizeof(1<<7):4 35 sizeof(1<<15):4 36 sizeof(1<<31):4 37 sizeof(1<<63):4 38 sizeof(1L<<7):4 39 sizeof(1L<<15):4 40 sizeof(1L<<31):4 41 sizeof(1L<<63):4 42 sizeof(1LL<<7):8 43 sizeof(1LL<<15):8 44 sizeof(1LL<<31):8 45 sizeof(1LL<<63):8 46 sizeof("hello"):6 47 sizeof("hello\\"):7 48 sizeof(EMPTY_STRUCT):1 49 sizeof(EMPTY_CLASS):1 50 sizeof(str1):6 51 sizeof(str2):10 52 sizeof(str3):4 53 sizeof(str4):4