1.基本数据类型大小的对比
- // C++Test.cpp : 定义控制台应用程序的入口点。
- //
- #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;
- }
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
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
2.为什么Windowsx64下long也为4byte?
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.指针的大小
- // C++Test.cpp : 定义控制台应用程序的入口点。
- //
- #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的大小
- // C++Test.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- using namespace std;
- //main
- int _tmain(int argc, _TCHAR* argv[])
- {
- string empty("");
- string name("hehe");
- string longstr("dfaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
- 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内部并不保存字符串本身,而是保存了一个指向字符串开头的指针。
原文链接: https://www.cnblogs.com/qiumingcheng/p/7824919.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/263376
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!