PVS-Studio, the static code analyzer, for the following bit of code
size_t const n = 4;
int a[n] = {};
reports:
V112 Dangerous magic number
4
used:...t const n = 4;. test.cpp 3
Although PVS-Studio is used with Visual Studio 2017 project and reports the same warning for both, 32 and 64 bit, those build configurations are not taken into account by the analyzer, AFAIU.
I would have expected the context to be analysed better and treat the code above as equivalent to this
int a[4] = {};
for which PVS-Studio does not issue any diagnostics.
In the case above is this dangerous magic number N used, a false positive?
What are the reasons the two code samples above are not analyzed as equivalent?
n = 4
and not with the use ofn
as an array size. – Orangeadeint a[4]
doesn't get warning. – Ovipositor4
. As an experiment, try "#define ARRAY_CAPACITY (4U), instead of
const size_t n = 4;`. You may need to configure the analyzer for C++. – Calcia[4]
. Complains about number4
is clear, but how on earth one is supposed to use constant/literal without putting there the4
;-) – Vidalint a[4]
is mentioned as special case that gets exempted from the check. Use 5 as your constant and there will be no warning. – Ovipositor//-V112
after line where warning is issued. – Ovipositor4
or0xffffffff
:-) FYI,n=4
comes from nanodbc library tests, what a coincidence! – Vidal