C/C++——基本資料型別的大小並且sizeof(int *) = 8

readyao發表於2016-02-24
#include <iostream>
using namespace std;

int main()
{
    cout << "sizeof(bool) = " << sizeof(bool) << endl;
    cout << "sizeof(char) = " << sizeof(char) << endl;
    cout << "sizeof(int) = " << sizeof(int) << endl;
    cout << "sizeof(float) = " << sizeof(float) << endl;
    cout << "sizeof(double) = " << sizeof(double) << endl;
    cout << "sizeof(long double) = " << sizeof(long double) << endl;

    enum e{a, b=3};

    cout << "sizeof(enum) = " << sizeof(e) << endl;
    cout << "sizeof(int *) = " << sizeof(int *) << endl;
    cout << "sizeof(char *) = " << sizeof(char *) << endl;


    return 0;
}


執行結果:

sizeof(bool) = 1
sizeof(char) = 1
sizeof(int) = 4
sizeof(float) = 4
sizeof(double) = 8
sizeof(long double) = 16
sizeof(enum) = 4
sizeof(int *) = 8
sizeof(char *) = 8

主要是在ubuntu64位下面sizeof(int *) 和 sizeof(char *)是8位元組,也就是地址是64位。

解釋是:在32位編譯器上sizeof(指標)是4位元組。在64位編譯器上sizeof(指標)是8位元組。32位地址最大定址為4G=2^32,64位最大定址為128T=2^64。




相關文章