天天看点

C++ 基本数据类型 的 字节数

闲聊c/c++: 各平台下基本数据类型的字节长度:​​https://www.jianshu.com/p/1116616940bb​​

32位 与 64位系统 数据类型

不同的平台上对不同的数据类型分配的字节数是不同的,一般的,数据类型的字节数是由编辑器决定的(编译期间决定数据类型长度)。

简单来说,平台就是 CPU+OS+Compiler,cpu的位是指一次性可处理的数据量是多少,1字节=8位,32位处理器可以一次性处理4个字节的数据量,依次类推。32位操作系统针对的32位的CPU设计。64位操作系统针对的64位的CPU设计。所以平台是三者的组合,它们的字节长相同时,效率最高。

下面是32位系统与64位系统各数据类型对比:

C++ 基本数据类型 的 字节数

除了 * 随操作系统子长变化而变化外,其他的都固定不变(32位和64位相比)

float 与 double 的 范围 和 精度

范围 :

        float和double的范围是由指数的位数来决定的。 

        float的指数位有8位,而double的指数位有11位,分布如下: 

float: 

        1bit(符号位) 8bits(指数位) 23bits(尾数位) 

double: 

        1bit(符号位) 11bits(指数位) 52bits(尾数位) 

于是,float 的指数范围为-127~+128,而 double 的指数范围为-1023~+1024,并且指数位是按补码的形式来划分的。 

其中负指数决定了浮点数所能表达的绝对值最小的非零数;而正指数决定了浮点数所能表达的绝对值最大的数,也即决定了浮点数的取值范围。 

float 的范围为-2^128 ~ +2^128,也即-3.40E+38 ~ +3.40E+38;

double 的范围为-2^1024 ~ +2^1024,也即-1.79E+308 ~ +1.79E+308。

精度 

float和double的精度是由尾数的位数来决定的。浮点数在内存中是按科学计数法来存储的,其整数部分始终是一个隐含着的“1”,由于它是不变的,故不能对精度造成影响。 

float:2^23 = 8388608,一共七位,这意味着最多能有7位有效数字,但绝对能保证的为6位,也即 float 的精度为6~7位有效数字; 

double:2^52 = 4503599627370496,一共16位,同理,double 的精度为15~16位。

1. 基本数据类型大小的对比

关于数据类型的大小,总是记不住,这里也算有个记录,顺便看一下 32位 和 64位 之间的差别:

32 和 64  选择

C++ 基本数据类型 的 字节数

测试程序

#include "stdafx.h"  
#include <iostream>  
#include <string>  
using namespace std;  
  
  
//main  
int _tmain(int argc, _TCHAR* argv[])  
{  
    cout << "sizeof(char):" << sizeof(char) << endl;  
    cout << "sizeof(short):" << sizeof(short) << endl;  
    cout << "sizeof(int):" << sizeof(int) << endl;  
    cout << "sizeof(long):" << sizeof(long) << endl;  
    cout << "sizeof(long long):" << sizeof(long long) << endl;  
    cout << "sizeof(unsigned int):" << sizeof(unsigned int) << endl;  
    cout << "sizeof(float):" << sizeof(float) << endl;  
    cout << "sizeof(double):" << sizeof(double) << endl;  
    void* pointer;  
    cout << "sizeof(pointer):" << sizeof(pointer) << endl;  
  
    system("pause");  
    return 0;  
}      

 win32下:

sizeof(char):1
sizeof(short):2
sizeof(int):4
sizeof(long):4
sizeof(long long):8
sizeof(unsigned int):4
sizeof(float):4
sizeof(double):8
sizeof(pointer):4      
C++ 基本数据类型 的 字节数

x64下:

sizeof(char):1
sizeof(short):2
sizeof(int):4
sizeof(long):4
sizeof(long long):8
sizeof(unsigned int):4
sizeof(float):4
sizeof(double):8
sizeof(pointer):8      
C++ 基本数据类型 的 字节数

32位 和 64位 系统在Windows下基本数据类型的大小都是一样的。只有指针的大小不一样!32位指针大小为4byte,而64位的指针大小为8byte。

注:Linux下,long 型是64位的,这一点是和Windows不同的地方。

PS:64位系统下是可以运行32位程序的。但是反过来的话是运行不了的。

2.为什么Windowsx64下 long 也为 4 byte?

我们知道,正常标准的话,long应该也是64位即8byte。但是在Windows下,我们的结果却是4byte。为什么会这样呢?这里引用MSDN的一段关于x64下的解释:

Platform SDK: 64-bit Windows Programming

Abstract Data Models

Every application and every operating system has an abstract data model. Many applications do not explicitly expose this data model, but the model guides the way in which the application's code is written. In the 32-bit programming model (known as the ILP32 model), integer, long, and pointer data types are 32 bits in length. Most developers have used this model without realizing it. For the history of the Win32? API, this has been a valid (although not necessarily safe) assumption to make.

In 64-bit Microsoft? Windows?, this assumption of parity in data type sizes is invalid. Making all data types 64 bits in length would waste space, because most applications do not need the increased size. However, applications do need pointers to 64-bit data, and they need the ability to have 64-bit data types in selected cases. These considerations led to the selection of an abstract data model called LLP64 (or P64). In the LLP64 data model, only pointers expand to 64 bits; all other basic data types (integer and long) remain 32 bits in length.

Initially, most applications that run on 64-bit Windows will have been ported from 32-bit Windows. It is a goal that the same source, carefully written, should run on both 32- and 64-bit Windows. Defining the data model does not make this task easier. However, ensuring that the data model affects only pointer data types is the first step. The second step is to define a set of new data types that allow developers to automatically size their pointer-related data. This allows data associated with pointers to change size as the pointer size changes from 32 bits to 64 bits. Basic data types remain 32 bits in length, so there is no change in the size of data on the disk, data shared over a network, or data shared through memory-mapped files. This relieves developers of much of the effort involved in porting 32-bit code to 64-bit Windows.

These new data types have been added to the Windows API header files. Therefore, you can start using the new types now. For more information, see The New Data Types.

简单解释一下:

我们编程时很少关注数据类型真正的大小,毕竟即使不关注这个也可以编程,而且我们习惯了Win32,到64位下,只有指针因为寻址需要是必须变成64位的,64位的指针寻址范围是0~2^64-1,而其他的数据类型基本已经够用,如果把所有数据类型变成64位,明显是浪费空间。再者,为了让32位和64位程序兼容运行,能少修改还是少修改,所以Windows仅将指针大小进行了修改。这样,程序可以兼容运行。

3.指针的大小

我们看看指针到底有多大?指向不同类型对象的指针大小是不是会有不同?看一个小例子:

#include "stdafx.h"  
#include <iostream>  
#include <string>  
using namespace std;  
  
class Test  
{  
    int num;  
    string name;  
};  
//一个函数指针  
typedef void(*pFunc)(void);  
void PrintHello(void)  
{  
    cout << "hello world" << endl;  
}  
//main  
int _tmain(int argc, _TCHAR* argv[])  
{  
    int* pInt;  
    void* pVoid;  
    Test* pTest = new Test();  
    pFunc pfunc = PrintHello;  
    cout << "sizeof(pInt):" << sizeof(pInt) << endl;  
    cout << "sizeof(pVoid):" << sizeof(pVoid) << endl;  
    cout << "sizeof(pTest):" << sizeof(pTest) << endl;  
    cout << "sizeof(pFunc):" << sizeof(pfunc) << endl;  
  
    system("pause");  
    return 0;  
}      

结果:

Win32下:

sizeof(pInt):4
sizeof(pVoid):4
sizeof(pTest):4
sizeof(pFunc):4
请按任意键继续. . .      

x64下:

sizeof(pInt):8
sizeof(pVoid):8
sizeof(pTest):8
sizeof(pFunc):8
请按任意键继续. . .      

可见,不管指针指向张三李四还是王二麻子,都是一样大的。能够影响指针大小的,还是位数。32位下指针大小为4,64位下指针的大小为8.

4.string的大小

关于string的大小,我们写一小段代码测试一下:

#include "stdafx.h"  
#include <iostream>  
#include <string>  
using namespace std;  
//main  
int _tmain(int argc, _TCHAR* argv[])  
{  
    string empty("");  
    string name("hehe");  
    string longstr("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");  
    cout << sizeof(empty) << endl;  
    cout << sizeof(name) << endl;  
    cout << sizeof(longstr) << endl;  
    cout << sizeof(string) << endl;  
    system("pause");  
    return 0;  
}      

结果:

Win32下:

28
28
28
28
请按任意键继续. . .      

x64下:

32
32
32
32
请按任意键继续. . .      

32位 和 64位下string 差 4byte,其实就是一个指针的差别。

string 内部并不保存字符串本身,而是保存了一个指向字符串开头的指针。