C++ 类型大小(32bit 与 64bit)

3 分钟阅读

C++ 类型大小 (32bit 与 64bit)

#include <iostream>

int main()
{
    std::cout << "---标准类型大小---" << std::endl;
    std::cout << "short size:              " << sizeof(short) << std::endl;
    std::cout << "int size:                " << sizeof(int) << std::endl;
    std::cout << "unsigned int size:       " << sizeof(unsigned int) << std::endl;
    std::cout << "long size:               " << sizeof(long) << std::endl;
    std::cout << "unsigned long size:      " << sizeof(unsigned long) << std::endl;
    std::cout << "long long size:          " << sizeof(long long) << std::endl;
    std::cout << "unsigned long long size: " << sizeof(unsigned long long) << std::endl;
    std::cout << "size_t size:             " << sizeof(size_t) << std::endl;
    std::cout << "double size:             " << sizeof(double) << std::endl;
    std::cout << "float size:              " << sizeof(float) << std::endl;
    std::cout << "char size:               " << sizeof(char) << std::endl;
    std::cout << "unsigned char size:      " << sizeof(unsigned char) << std::endl;
    std::cout << "signed char size:        " << sizeof(signed char) << std::endl;
    std::cout << "pointer size:            " << sizeof(void*) << std::endl;

    std::cout << std::endl;
    std::cout << "---自定义类型大小---" << std::endl;
    std::cout << "SHORT(short) size:               " << sizeof(short) << std::endl;
    std::cout << "INT(int) size:                   " << sizeof(int) << std::endl;
    std::cout << "LONG(long) size:                 " << sizeof(long) << std::endl;
    std::cout << "UINT8(unsigned char) size:       " << sizeof(unsigned char) << std::endl;
    std::cout << "UINT16(unsigned short) size:     " << sizeof(unsigned short) << std::endl;
    std::cout << "UINT32(unsigned int) size:       " << sizeof(unsigned int) << std::endl;
    std::cout << "UINT64(unsigned long long) size: " << sizeof(unsigned long long) << std::endl;
    std::cout << "INT8(char) size:                 " << sizeof(char) << std::endl;
    std::cout << "INT16(short) size:               " << sizeof(short) << std::endl;
    std::cout << "INT32(int) size:                 " << sizeof(int) << std::endl;
    std::cout << "INT64(long long) size:           " << sizeof(long long) << std::endl;
    std::cout << "FLOAT(float) size:               " << sizeof(float) << std::endl;
    std::cout << "DOUBLE(double) size:             " << sizeof(double) << std::endl;
    std::cout << "CHAR(char) size:                 " << sizeof(char) << std::endl;
    std::cout << "BOOL(int) size:                  " << sizeof(int) << std::endl;
    std::cout << "BYTE(unsigned char) size:        " << sizeof(unsigned char) << std::endl;
    std::cout << "HANDLE(void*) size:              " << sizeof(void*) << std::endl;

    return 0;
}
类型 VS2010 32bit VS2010 64bit VS2019 32bit VS2019 64bit Linux 32bit Linux 64bit 备注
short 2 2 2 2   2  
int 4 4 4 4   4  
long 4 4 4 4   8 不同
long long 8 8 8 8   8  
unsigned short 2 2 2 2   2  
unsigned int 4 4 4 4   4  
unsigned long 4 4 4 4   8 不同
unsigned long long 8 8 8 8   8  
size_t 4 8 4 8   8 不同
char 1 1 1 1   1  
signed char 1 1 1 1   1  
unsigned char 1 1 1 1   1  
float 4 4 4 4   4  
double 8 8 8 8   8  
pointer 4 8 4 8   8 不同
SHORT(short) 2 2 2 2   2  
INT(int) 4 4 4 4   4  
LONG(long) 4 4 4 4   8 不同
UINT8(unsigned char) 1 1 1 1   1  
UINT16(unsigned short) 2 2 2 2   2  
UINT32(unsigned int) 4 4 4 4   4  
UINT64(unsigned long long) 8 8 8 8   8  
INT8(char) 1 1 1 1   1  
INT16(short) 2 2 2 2   2  
INT32(int) 4 4 4 4   4  
INT64(long long) 8 8 8 8   8  
FLOAT(float) 4 4 4 4   4  
DOUBLE(double) 8 8 8 8   8  
CHAR(char) 1 1 1 1   1  
BOOL(int) 4 4 4 4   4  
BYTE(unsigned char) 1 1 1 1   1  
HANDLE(void*) 4 8 4 8   8 不同

结论:

  1. size_tpointerlongunsigned longHANDLE这三种类型在32bit64bit的大小有差别。

更新时间: