X86和X64环境下的基本类型所占用的字节大小

时间:2021-07-16 01:00:39

同样的程序代码,使用Visual Studio 进行编译,当目标平台分别为x86或x64环境时,其编译结果是不同的。在x86环境下,指针都是4个字节的;而在x64环境下,指针都是8字节的。测试代码如下:

#include<iostream>

using namespace std;

int main(int argc, char** argv)
{
    cout
        << "sizeof(char)=" << sizeof(char) << endl
        << "sizeof(char*)=" << sizeof(char*) << endl
        << "sizeof(short)=" << sizeof(short) << endl
        << "sizeof(short*)=" << sizeof(short*) << endl
        << "sizeof(int)=" << sizeof(int) << endl
        << "sizeof(int*)=" << sizeof(int*) << endl
        << "sizeof(long)=" << sizeof(long) << endl
        << "sizeof(long*)=" << sizeof(long*) << endl
        << "sizeof(long long)=" << sizeof(long long) << endl
        << "sizeof(long long*)=" << sizeof(long long*) << endl
        << "sizeof(float)=" << sizeof(float) << endl
        << "sizeof(float*)=" << sizeof(float*) << endl
        << "sizeof(double)=" << sizeof(double) << endl
        << "sizeof(double*)=" << sizeof(double*) << endl;
    return 0;
}
目标平台为X86,编译后运行结果:

X86和X64环境下的基本类型所占用的字节大小

目标平台为X64,编译后运行结果:

X86和X64环境下的基本类型所占用的字节大小