|
|
|||
![]() PVS-Studio Static Code Analyzer for 64-bit and parallel C/C++ code
|
|||
![]() ![]() ![]() ![]() ![]()
02.09.2010
Feeling the new Intel Parallel Studio XE 2011 beta So I've gotten to try the C++ compiler included into Intel Parallel Studio XE 2011 beta at last.»
30.08.2010
Five days for fixing a two-character error, or a myth of almighty technologies aiding software development In this blog, you may often read posts about how this or that software tool or software development technology helps make fewer errors, find them faster and correct them easier.»
30.08.2010
d'Artagnan and Internet, or working on the problem of bad links Friends, it is high time we stopped considering links only in the context of their number and buying/ selling and counting PR of the site they are laid out on.» ![]()
22.07.2010
Using PVS-Studio with continuous integration systems This article illustrates techniques required to employ the use of PVS-Studio static code analyzer together with continuous integration systems.»
06.07.2010
Comparing capabilities of PVS-Studio and Visual Studio 2010 in detecting defects in 64-bit programs In the article, we will compare three mechanisms of code analysis from the viewpoint of detecting 64-bit errors: the Visual C++ 2010 compiler, the Code Analysis for C/C++ component included into Visual Studio 2010 and Viva64 analyzer included into PVS-Studio 3.60.»
29.06.2010
A Collection of Examples of 64-bit Errors in Real Programs
This article is the most complete collection of examples of 64-bit errors in the C and C++ languages.» ![]() |
Knowledge Base![]() Difference between %p and %xFunctions belonging to the printf function family have the type specifiers "%p" and "%x".
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: 0018FF20 18FF20 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 corresponding 64-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=80000040
use %X: b=40010040
use %p: a=0000000080000040
use %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 string
int *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 analyzer Viva64. | ||