C/C++知识点之64位平台C/C++容易犯的错误
小标 2018-12-03 来源 : 阅读 1018 评论 0

摘要:本文主要向大家介绍了 C/C++知识点之64位平台C/C++容易犯的错误,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了 C/C++知识点之64位平台C/C++容易犯的错误,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

64位平台的介绍


IA-64 is a 64-bit microprocessor architecture developed by Intel and Hewlett Packard companies together. It is implemented in Itanium and Itanium 2 microprocessors. To learn more about the architecture IA-64 see the following Wikipedia article "Itanium".
Intel 64 (EM64T / AMD64 / x86-64 / x64) is an extension of x86 architecture with full backward compatibility. There are many variants of its name, and it causes some confusion, but all these names mean the same thing: x86-64, AA-64, Hammer Architecture, AMD64, Yamhill Technology, EM64T, IA-32e, Intel 64, x64. To learn how so many names appeared see the article in Wikipedia: "X86-64".


64位平台的优势

64-bit address space;
an extended register set;
a command set familiar to developers;
capability to launch obsolete 32-bit applications in a 64-bit operating system;
capability to use 32-bit operating systems.


Win64 Program model

Like in Win32, the size of a page in Win64 is 4 Kbytes. The first 64 Kbytes of the address space are never displayed, so the lowest correct address is 0x10000. Unlike Win32, system DLL‘s take more than 4 Gbytes.
Compilers for Intel 64 have one peculiarity: they can use registers with great efficiency to pass parameters into functions, instead of using the stack. This allowed the Win64 architecture developers to get rid of notions such as a calling convention. In Win32, you may use various conventions: __stdcall, __cdecl, __fastcall, etc. In Win64, there is only one calling convention. Here is an example of how four arguments of integer type are passed through registers:

RCX: the first argument
RDX: the second argument
R8: the third argument
R9: the fourth argument

The arguments following the first four integers are passed through the stack. To pass float arguments XMM0-XMM3 registers are used as well as the stack.
The difference in the calling conventions makes it impossible to use both 64-bit and 32-bit code in one program. In other words, if an application has been compiled for the 64-bit mode, all the libraries (DLL) being used must also be 64-bit.
Passing parameters through registers is one of the innovations that make 64-bit programs faster than 32-bit ones. 


64位平台应用程序的性能

After being recompiled for a 64-bit system a program can use huge amounts of memory and its speed will increase in 5-15%. 5-10% of speed gain is achieved due to architectural features of the 64-bit processor, for example, a larger number of registers. And another 1-5% performance gain is determined by the absence of the WoW64 layer that translates calls between 32-bit applications and the 64-bit operating system.
For example, Adobe company says that a new 64-bit "Photoshop CS4" is 12% faster than its 32-bit version".


 
64位平台下容易犯的错误

sizeof()的返回值在64位平台下占用8bytes,而不是32位平台下的4bytes;
size_t类型,指针类型等在64位平台下占用8bytes,而不是32位平台下的4bytes;
printf函数中,“%d” “%u” “%x”等模式只能打印出32bit类型的数,如果要打印出64bit的数,需要添加"I"(Windows平台)或“z”(Linux平台),譬如“%Id” “%Iu” “%Ix”,“%zd” “%zu” “%zx”;
整数常量,譬如 1, 在64位平台下的类型属于 int,因此占用4bytes,那么在进行移位操作时,需要注意这一点;
移位操作时,被移位的变量会被先强制转换成signed,然后进行移位操作,譬如下面的代码:  


struct BitFieldStruct {
  unsigned short a:15;
  unsigned short b:13;
};
BitFieldStruct obj;
obj.a = 0x4000;
size_t addr = obj.a << 17; //Sign Extension
printf("addr 0x%Ix\n", addr);
//Output on 32-bit system: 0x80000000
//Output on 64-bit system: 0xffffffff80000000
// obj.a is converted into signed int before shift.


 地址加法时,很容易出错,譬如如下代码

int A = -2;
unsigned B = 1;
int array[5] = { 1, 2, 3, 4, 5 };
int *ptr = array + 3;
ptr = ptr + (A + B); //Invalid pointer value on 64-bit platform
printf("%i\n", *ptr); //Access violation on 64-bit platform

原因如下:  



Let us follow the algorithm of calculating the expression "ptr + (A + B)":

According to C++ rules, the variable A of the type int is converted to unsigned.
A and B are summed and we get the value 0xFFFFFFFF of unsigned type.
The expression "ptr + 0xFFFFFFFFu" is calculated.

The result of this process depends upon the size of the pointer on a particular architecture. If the addition takes place in the 32-bit program, the expression is equivalent to "ptr - 1" and the program successfully prints the value "3". In the 64-bit program, the value 0xFFFFFFFFu is fairly added to the pointer. As a result, the pointer gets far outside the array while we encounter some troubles when trying to get access to the item by this pointer.
Like in the first case, we recommend you to use only memsize-types in pointer arithmetic to avoid the situation described above. Here are one ways to correct the code:

ptr = ptr + (ptrdiff_t(A) + ptrdiff_t(B));
ptrdiff_t A = -2;
size_t B = 1;
...
ptr = ptr + (A + B);



 
指针类型的强制转换也容易导致错误

int array[4] = { 1, 2, 3, 4 };
enum ENumbers { ZERO, ONE, TWO, THREE, FOUR };
//safe cast (for MSVC)
ENumbers *enumPtr = (ENumbers *)(array);
cout << enumPtr[1] << " ";
//unsafe cast
size_t *sizetPtr = (size_t *)(array);
cout << sizetPtr[1] << endl;
//Output on 32-bit system: 2 2
//Output on 64-bit system: 2 17179869187=0x0000000400000003

原因:在64-bit系统中,size_t 占8bytes,因此,sizetPtr[1]指向的是内存各占4btyes的数字3和4拼接起来的内存块,也就是0x0000000400000003

 Storage of integer values in double

size_t a = size_t(-1);
double b = a;
--a;
--b;
size_t c = b; // x86: a == c
              // x64: a != c

原因:

This code may be justified when it is executed on a 32-bit system because the type double has 52 significant bits and can store a 32-bit integer value without loss. But when you save a 64-bit integer number into double, the exact result will be lost.

 

 

 
结构体占用的内存字节数

struct MyStruct
{
  bool m_bool;
  char *m_pointer;
  int m_int;
};

// x86: sizeof(MyStruct) = 12
// x64: sizeof(MyStruct) = 24


struct MyStruct

  char *m_pointer;
  int m_int;
  bool m_bool;
};

// x86: sizeof(MyStruct) = 12
// x64: sizeof(MyStruct) = 16

【 字节对齐的窍门】:The process of optimizing a field arrangement may seem complicated. But there is a very simple and very effective method: you just need to arrange the fields in decreasing order of their sizes.

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程