We are using Visual Studio 2015 Update 3 + Cumulative Servicing Release.
The following static code analyzer warning appears always at a specific situation:
operatornew.cpp(5): warning C28182: Dereferencing NULL pointer. 'a' contains the same NULL value as 'Temp_value_#2119' did.
- A pointer is dereferenced
- That pointer was allocated using array operator new
- and zero-initialization
See MWE here:
#include <iostream>
int main(int, char**)
{
int * a = new int[400]();
a[0] = 1;
int * b = new int[400];
b[0] = 1;
std::cout << a[0] << std::endl;
std::cout << b[0] << std::endl;
delete[] a;
delete[] b;
return 0;
}
SCA warning is emmited at line 5.
But when derefencing pointer 'b' - which does not use zero-initialitation - there is no warning.
The warning says, that pointer 'a' is NULL when allocation failed. I would rather expect an exception on allocation failure. So I think pointer 'a' is never NULL in this example.
Is this static code analyzer warning a false positive?
Is there an exception std::bad_alloc when using array operator new with zero-initialization (and MSVC15+Update3)?