Difference between %p and %x
来源:互联网 发布:hadoop编译java文件 编辑:程序博客网 时间:2024/06/05 06:54
Cite from http://www.viva64.com/en/k/0019/
Difference between %p and %x
Functions belonging to the printf function family have the type specifiers "%p" and "%x".
- "x" and "X" serve to output a hexadecimal number. "x" stands for lower case letters (abcdef) while "X" for capital letters (ABCDEF).
- "p" serves to output a pointer. It may differ depending upon the compiler and platform.
One specifier is often used instead of another on 32-bit systems, but it is a mistake. Here is an example:
int a = 10;int *b = &a;printf("%p\n",b);printf("%X\n",b);
On a Win32 system, the following result will be printed:
0018FF2018FF20
As you may see, the output results for "%p" and "%X" are rather similar. This similarity leads to inaccuracy in the code and this, in its turn, results in errors occurring when you port a program to a 64-bit platform. Most often it is "%X" that is used instead of "%p" to output the value of a pointer and it results in printing a wrong value if the object is situated outside the four less significant Gbytes of the address space. Let us consider the corresponding64-bit version of this program:
size_t Gb = 1024*1024*1024;char *a = (char *)malloc(2 * Gb * sizeof(char));char *b = (char *)malloc(2 * Gb * sizeof(char));printf("use %%X: a=%X\n", a);printf("use %%X: b=%X\n", b);printf("use %%p: a=%p\n", a);printf("use %%p: b=%p\n", b); use %X: a=80000040use %X: b=40010040use %p: a=0000000080000040use %p: b=0000000140010040
The pointer value "b" is printed incorrectly when using "%X". To make sure that such errors are quite real, see the post "Atavisms in large systems 2".
Here is one more example. Although it looks strange, the code given here in an abridged form was used in a real application in the UNDO/REDO subsystem:
// Here the pointers were saved in the form of a stringint *p1, *p2;....char str[128];sprintf(str, "%X %X", p1, p2);// In another function this string was processed// in this way:void foo(char *str){ int *p1, *p2; sscanf(str, "%X %X", &p1, &p2); // The result is incorrect values of pointers p1 and p2. ...}
Manipulation with the pointers using "%X" resulted in an incorrect program behavior on a 64-bit system. Note that such errors might occur very rarely. To diagnose these and other similar flaws it is good to use the analyzerViva64.
References
- Lessons on development of 64-bit C/C++ applications. Lesson 10. Pattern 2. Functions with variable number of arguments.
- Knowledge Base. How to correctly print a value of the types __int64, size_t, and ptrdiff_t.
- Difference between %p and %x
- difference between struts1.x and Struts2.x
- Is there any difference between List x; and List x();?
- The difference between (()) and ().
- difference between "<" and "<<"
- Difference Between typedef and #define
- Difference between PCDATA and CDATA
- Difference between CComPtr and CComQIPtr
- Difference between RVCT and GCCE
- Difference between RVCT and GCCE
- difference between slist and list.
- difference between stack and heap
- Difference between replace and translate
- Difference between PrintWriter and PrintStream
- difference between Cookie and Session
- Difference Between Buffer and Cache
- Difference between Statement and preparedStatement
- Difference between .DLL And .LIB
- 系统性能评价中的几个指标
- 关于模拟盘子的堆栈实现(Crack the interview 例题)之暑假学习记录
- 负载均衡设备选型计算参考
- 用U盘或移动硬盘安装Windows7 (超简单制作Win7安装U盘方法)
- Java 类反汇编命令
- Difference between %p and %x
- LINUX常用命令
- java 对接口的一点理解
- hdu 1007 (空间最近点对)
- C语言输出一个汉字
- Java中去除字符串两边空格拦截器StringTrimInterceptor
- 以太网帧最小字节数以及以太网碰撞
- Android进行项目设置的时候初始设置选项含义
- 我的分户热计量系统