64位平台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:;
unsigned short b:;
};
BitFieldStruct obj;
obj.a = 0x4000;
size_t addr = obj.a << ; //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 = -;
unsigned B = ;
int array[] = { , , , , };
int *ptr = array + ;
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 = -;
size_t B = ;
...
ptr = ptr + (A + B);
指针类型的强制转换也容易导致错误
int array[] = { , , , };
enum ENumbers { ZERO, ONE, TWO, THREE, FOUR };
//safe cast (for MSVC)
ENumbers *enumPtr = (ENumbers *)(array);
cout << enumPtr[] << " ";
//unsafe cast
size_t *sizetPtr = (size_t *)(array);
cout << sizetPtr[] << 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(-);
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.
64位平台的劣势:内存开销偏大
When a program is compiled in the 64-bit mode, it starts consuming more memory than its 32-bit version. This increase often stays unnoticed, but sometimes memory consumption may grow twice. The growth of memory consumption is determined by the following factors:
- larger memory amounts to store some objects, for example pointers;
- changes of the rules of data alignment in structures;
- growth of stack memory consumption.
We can often tolerate the growth of main memory consumption - the advantage of 64-bit systems is that very large amount of memory available to user. It is quite okay if a program takes 300 Mbytes on a 32-bit system with 2 Gbytes of memory and 400 Mbytes on a 64-bit system with 8 Gbytes of memory. In relative units, it means that the program takes three times less memory available on a 64-bit system. So it is unreasonable to strike against the growth of memory consumption we have described - it is easier to add just a bit more memory.
But there is a disadvantage of this growth. It is related to performance loss. Although the 64-bit program code is faster, extraction of larger data amounts from memory might cancel all the advantages and even reduce performance. The operation of transferring data between the memory and microprocessor (cache) is not very cheap.
One of the ways to reduce the memory being consumed is optimization of data structures we have told you about in Lesson 23.
Another way of saving memory is to use more saving data types. For instance, if we need to store a lot of integer numbers and we know that their values will never exceed UINT_MAX, we may use the type "unsigned" instead of "size_t".
[参考文献]:
64位平台C/C++容易犯的错误的更多相关文章
- SWMM代码移植到64位平台
在32位平台上运行SWMM模型,当节点数量到达60万以上的时候,模型运行占用内存接近1.85G的时候就会因为内存不够而无法计算.这种情况还是单独运行SWMM.exe的时候出现,如果采用SWMM.DLL ...
- 64位平台C/C++开发注意事项(转载)
转自http://coolshell.cn/articles/3512.html 在http://www.viva64.com/en/l/上例出了28个在64位平台上使用C/C++开发的注意事项,对于 ...
- 64位平台支持大于2 GB大小的数组
64位平台支持大于2 GB大小的数组 64位平台.NET Framework数组限制不能超过2GB大小.这种限制对于需要使用到大型矩阵和向量计算的工作人员来说,是一个非常大问题. 无论RAM容量有多大 ...
- 64位平台C/C++开发注意事项
在http://www.viva64.com/en/l/上例出了28个在64位平台上使用C/C++开发的注意事项,对于进入64位时代的程序员应该去看看这28个事项,这些英文读物对于有C/C++功底的朋 ...
- NET Framework 4.5新特性 (三)64位平台支持大于2 GB大小的数组
64位平台.NET Framework数组限制不能超过2GB大小.这种限制对于需要使用到大型矩阵和向量计算的工作人员来说,是一个非常大问题. 无论RAM容量有多大有多少,一旦你使用大型矩阵和向量计算工 ...
- 手把手教你写Windows 64位平台调试器
本文网页排版有些差,已上传了doc,可以下载阅读.本文中的所有代码已打包,下载地址在此. ------------------------------------------------------- ...
- win7 64位平台编译的程序在XP 32位平台无法运行的解决方法
win7 64位平台编译的程序在XP 32位平台无法运行的解决方法 vs2010的开发环境,制作了一个DLL库.但DLL在XP 32位平台一直无法使用.解决方法如下: 右键项目,属性->配置属性 ...
- 查看Pyton的版本号和32/64位平台
怎么查看Python的版本号?使用的Python是32位还是64位的?用以下两条Python 指令就可以知道. 方法1:通过Python代码查看 import platform import sys ...
- xsocks 64位平台下编译问题小记
1.src/common/public.h uint32_t lpParameter 改为 void* lpParameter; 2.SocksMgr.cpp DWORD WINAPI CSocksM ...
随机推荐
- java数据结构分析
java数据结构分析 此文章内容参考于:http://www.cnblogs.com/ysocean/ 一.数据结构总览图 1.数组 2.链表 3.栈 4.队列 5.二叉树 6.堆 7.散列 8.红黑 ...
- python-django(环境配置)
1.配置虚拟环境 <1>.pip install virtualenv 安装创建虚拟环境的工具 <2>.pip install virtualenvwrappe ...
- 微信小程序开发过程问题总汇
之前在开发一个控车小程序,把过程中稍微需要搜索的问题做了记录. 1. 关键词:本地资源图片无法通过WXSS获取 描述:做小程序开发的时候,如果你需要使用图片作为背景,也就是想使用background- ...
- MySQL加入log_bin报错
MySQL中二进制日志功能默认是关闭的,查看各种开启方式后,确定在配置文件中加入如下配置来开启该功能: [root@bogon /]# more /etc/my.cnf [mysqld] datadi ...
- python基础 (装饰器,内置函数)
https://docs.python.org/zh-cn/3.7/library/functions.html 1.闭包回顾 在学习装饰器之前,可以先复习一下什么是闭包? 在嵌套函数内部的函数可以使 ...
- Mysql必知必会 第三章 使用Mysql
第三章 使用Mysql SQL语句和大小写 请注意,SQL语句不区分大小写,因此SELECT与select是相同的.同样,写成Select也没有关系.许多SQL开发人员喜欢对所有SQL关键字使用大写, ...
- 洛谷P3916||图的遍历||反向建图||链式前向星||dfs
题目描述 给出 NN 个点, MM 条边的有向图,对于每个点 vv ,求 A(v)A(v) 表示从点 vv 出发,能到达的编号最大的点. 解题思路 看起来很简单的一道题, 但我依然调了一天,我还是太菜 ...
- 一、PyQt5基础概念与安装配置
一.初识PyQt5 对于桌面程序开发,用户图形界面(GUI)的设计非常重要.一款美观.易用的用户界面可以很大程度上提高对使用这的友好度.由于Python最初是作为脚本语言开发,并没有GUI功能.但Py ...
- Python学习——1
我是一名刚入IT行业的小白,目前主要是做网络运维这一块.曾经总是认为我是做网络运维的,学习代码干啥啊?后来就慢慢发现,传统的运维方式让我的效率好像不如别人效率高,关键还TM看别人比我更轻松.每一个网络 ...
- Atom使用
cmd-r 通过方法名查找 ctrl-option-g 跳转至光标所在方法或结构 安装 last-cursor-position 后 alt-- 返回至光标上一次所在位置
