This piece of code (note the commented line):
#include <malloc.h>
#pragma warning(error: 4701)
int main(){
char buffer[1024];
//buffer[0] = 0;
void *p;
int size = 1;
if (size < 2)
p = malloc(size);
free(p); // C4701
return 0;
}
Gives the following warning (as expected):
f:\d\warning.cpp(13) : error C4701: potentially uninitialized local variable 'p' used
However, when I uncomment the assignment in main()
, the warning is no longer given. I am compiling with /RTC1
command line option to enable run-time error checks:
cl.exe /RTC1 warning.cpp
I've tried the latest 64-bit versions of compilers from Visual C++ 2013 and 2015. Both are producing the same behaviour.
Question is: is this a compiler bug, or is there an explanation for this? Microsoft's documentation mentions that /RTC1 might give run-time error in places where C4701 is given, but it says nothing about the warning being suppressed.
EDIT: The puzzling part is that the warning disappears only when buffer[0] = 0;
is not in comment.
/RTC1
switch. – Garzonp
is in fact initialized when you use /RTC1 so this is expected. That's accurate enough, be sure to mention that this is not the behavior you prefer. – Declanp
! – Stereophotography#pragma warning(error: 4701)
statement overrules the /W option for specified warning. – Stereophotography