Customers




Developers Resources

Blog

05.02.2010 /Wp64 switch and template processing error
While maintaining the analyzer Viva64 (included into PVS-Studio) we often comment upon the switch /Wp64 of Microsoft Visual C++.»

03.02.2010 Parallel notes N1 - OpenMP technology
In the next few posts we will tell you about using multi-core processors in practice.»

29.01.2010 64-bit technologies - one more trend in the modern software
In the blogs and forums, there is much discussion of multi-core processors as an evident step of computer system development.»

Blog RSS

News

2.02.2010 "Lessons of 64-bit C/C++ software development" are now available on our site.»

1.02.2010 PVS-Studio 3.45 New Version Released!»

21.01.2010 PVS-Studio 3.44 New Version Released!»

News RSS

Articles

10.12.2009 PVS-Studio FAQ
This paper contains some questions and answers about PVS-Studio code analyzer by OOO "Program Verification Systems".»

09.12.2009 VivaCore FAQ
This paper contains some questions and answers about VivaCore C/C++ code analysis library by OOO "Program Verification Systems"»

23.11.2009 PVS-Studio: using the function "Mark as False Alarm"
The article describes and demonstrates by an example the use of PVS-Studio 3.40 new function "Mark as False Alarm". »

Articles RSS

Documentation

PVS-Studio Send comments on this topic.
V112. Dangerous magic number used

The analyzer found the use of a dangerous magic number. The possible error may consist in the use of numeric literal as special values or size of memsize type.

Let's examine the first example.

 
size_t ArraySize = N * 4;
size_t *Array = (size_t *)malloc(ArraySize);

A programmer while writing the program relied on that the size size_t will be always equal 4 and wrote the calculation of the array size "N * 4". This code dose not take into account that size_t on the 64-bit system will have 8 bytes and will allocate less memory than it is necessary. The correction of the code consists in the use of sizeof operator instead of a constant 4.

 
size_t ArraySize = N * sizeof(size_t);
size_t *Array = (size_t *)malloc(ArraySize);

The second example.

 
size_t n = static_cast<size_t>(-1);
if (n == 0xffffffffu) { ... }

Sometimes as an error code or other special marker the value "-1" is used which is written as "0xffffffff". On the 64-bit platform the written expression is incorrect and one should evidently use the value "-1".

 
size_t n = static_cast<size_t>(-1);
if (n == static_cast<size_t>(-1)) { ... }

Let's list magic numbers which may influence the efficiency of an application while porting it on the 64-bit system and due to this are diagnosed by analyzer.

Value
Description
4
Number of bytes in a type
32
Number of bits in a type
0x7fffffff
The maximum value of a 32-bit signed variable. Mask for zeroing of the high bit in a 32-bit type
0x80000000
The minimum value of a 32-bit signed variable. Mask for allocation of the high bit in a 32-bit type
0xffffffff
The maximum value of a 32-bit variable. An alternative record -1 as an error sign

You should study the code thoroughly in order to see if there are magic constants and replace them with safe constants and expressions. For this purpose you may use sizeof() operator, special value from <limits.h>, <inttypes.h> etc.

In some cases magic constants are considered unsafe. For example, there will be no warning on this code:

 
float Color[4];                          

See also:

memsize type

Limitation