C++ 32&64位操作系统的区别

一.32 & 64bit的概念

所谓32位和64位,指CPU的字长,其实主要是 GPRS(General Purpose Regisers,通用寄存器)的数据宽度。电脑内部都是2进制运算,CPU一次处理数据的能力也是2的倍数。一次处理的数据越大,该电脑处理信息的能力就越强。所谓32位处理器就是一次只能处理32位,即4 bytes 的数据,而64位处理器一次就能处理64bit,即8 bytes 的数据。

CPU从32bit 到64bit,主要的变化如下:

  1. 处理能力的提升。比如原来要两个周期做的事,现在一个周期就能搞定
  2. 64bit CPU寻址能力更强。32bit CPU只能寻址2^32 (4G)内存(所以,如果是32bit系统,搞8G内存实际是没用的)。而64bit CPU理论上的寻址能力是2^64 (约1700TB),目前的硬件还达不到这个水平
  3. 数据精度的提升。一些数据类型可用更大的字长表示,可表示更大范围或更精确的数。CPU分32bit & 64bit。在此基础上就有了32bit操作系统和64bit操作系统。进而还有32bit程序和64bit程序

那么在编程的时候,怎么选择Win32还是x64:
打开VS,默认的工程设置是32位的。要修改的话可以通过编译选项进行修改:
在这里插入图片描述

二.32 & 64bit下各种数据类型大小对比

  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;
}

Run result:

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

可以看到,除了pointer,其余数据类型大小都一致
注:Linux下,long型是64bit,这一点与Windows不同
PS:64bit系统下是可以运行32位程序。但反过来不行。在32bit机器上运行64bit程序,会弹出类似如下提示:
在这里插入图片描述

  1. 为什么Win64下long也为4byte
    正常标准的话,long应该是64bit。但在Windows下却是4byte。Why?这里引用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.

为了让32位和64位程序兼容运行,能少修改还是少修改,所以Windows仅将指针大小进行了修改。这样,程序可以兼容运行。

  1. string的大小
    关于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;
}

Result:

Win32下:
28
28
28
28

x64下:
32
32
32
32

32位和64位下string差4byte,其实就是一个指针的差别。string内部并不保存字符串本身,而是保存了一个指向字符串开头的指针。

引用文章:C++ 32位64位的区别

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值