32位64位下各种数据类型⼤⼩的对⽐
1.基本数据类型⼤⼩的对⽐
关于数据类型的⼤⼩,总是记不住,这⾥也算有个记录,顺便看⼀下32位和64位之间的差别:
我写了⼀⼩段测试代码:
[cpp]
1. // C++Test.cpp : 定义控制台应⽤程序的⼊⼝点。
2. //
3.
4. #include "stdafx.h"
5. #include <iostream>
6. #include <string>
7. using namespace std;
8.
9.
10. //main
11. int _tmain(int argc, _TCHAR* argv[])
12. {
13. cout << "sizeof(char):" << sizeof(char) << endl;
14. cout << "sizeof(short):" << sizeof(short) << endl;
sizeof 指针15. cout << "sizeof(int):" << sizeof(int) << endl;
16. cout << "sizeof(long):" << sizeof(long) << endl;
17. cout << "sizeof(long long):" << sizeof(long long) << endl;
18. cout << "sizeof(unsigned int):" << sizeof(unsigned int) << endl;
19. cout << "sizeof(float):" << sizeof(float) << endl;
20. cout << "sizeof(double):" << sizeof(double) << endl;
21. void* pointer;
22. cout << "sizeof(pointer):" << sizeof(pointer) << endl;
23.
24. system("pause");
25. return 0;
26. }
看⼀下结果:
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
请按任意键继续. . .
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
32位和64位系统在Windows下基本数据类型的⼤⼩都是⼀样的。只有指针的⼤⼩不⼀样!32位指针⼤⼩为4byte,⽽64位的指针⼤⼩为
8byte。
注:Linux下,long型是64位的,这⼀点是和Windows不同的地⽅。
PS:64位系统下是可以运⾏32位程序的。但是反过来的话是运⾏不了的。我在⼀台32位的机器上运⾏上⾯的64位的程序,就会弹出如下提⽰:
2.为什么Windowsx64下long也为4byte?
我们知道,正常标准的话,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.指针的⼤⼩
我们看看指针到底有多⼤?指向不同类型对象的指针⼤⼩是不是会有不同?看⼀个⼩例⼦:
[cpp]
1. // C++Test.cpp : 定义控制台应⽤程序的⼊⼝点。
2. //
3.
4. #include "stdafx.h"
5. #include <iostream>
6. #include <string>
7. using namespace std;
8.
9. class Test
10. {
11. int num;
12. string name;
13. };
14. //⼀个函数指针
15. typedef void(*pFunc)(void);
16. void PrintHello(void)
17. {
18. cout << "hello world" << endl;
19. }
20. //main
21. int _tmain(int argc, _TCHAR* argv[])
22. {
23. int* pInt;
24. void* pVoid;
25. Test* pTest = new Test();
26. pFunc pfunc = PrintHello;
27. cout << "sizeof(pInt):" << sizeof(pInt) << endl;
28. cout << "sizeof(pVoid):" << sizeof(pVoid) << endl;
29. cout << "sizeof(pTest):" << sizeof(pTest) << endl;
30. cout << "sizeof(pFunc):" << sizeof(pfunc) << endl;
31.
32. system("pause");
33. return 0;
34. }
结果:
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的⼤⼩,我们写⼀⼩段代码测试⼀下:
[cpp]
1. // C++Test.cpp : 定义控制台应⽤程序的⼊⼝点。
2. //
3.
4. #include "stdafx.h"
5. #include <iostream>
6. #include <string>
7. using namespace std;
8. //main
9. int _tmain(int argc, _TCHAR* argv[])
10. {
11. string empty("");
12. string name("hehe");
13. string longstr("dfaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
14. cout << sizeof(empty) << endl;
15. cout << sizeof(name) << endl;
16. cout << sizeof(longstr) << endl;
17. cout << sizeof(string) << endl;
18. system("pause");
19. return 0;
20. }
结果:
Win32下:
28
28
28
28
请按任意键继续. . .
x64下:
32
32
32
32
请按任意键继续. . .
32位和64位下string差4byte,其实就是⼀个指针的差别。string内部并不保存字符串本⾝,⽽是保存了⼀个指向字符串开头的指针。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论