Warning C4701 is sometimes suppressed when compiling with / RTC1

This piece of code (pay attention to 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 indicated. I am compiling with the /RTC1 command line /RTC1 to enable runtime error checking :

 cl.exe /RTC1 warning.cpp 

I tried the latest 64-bit versions of compilers from Visual C ++ 2013 and 2015. Both of them produce the same behavior.

Question: is this a compiler error, or is there an explanation for this? The Microsoft documentation mentions that / RTC 1 may give a runtime error in places where C4701 is specified, but it does not say anything about suppressing the warning.

EDIT: The incomprehensible part is that the warning disappears only when buffer[0] = 0; does not comment.

+7
c ++ visual-c ++ compiler-bug cl
source share
1 answer

There are many situations where something is not optimal, perhaps a buggy or even undefined, where the compiler makes it very difficult to detect. Therefore, you should not rely on warnings (and / or runtime errors triggered by compiler tools) to give you the full truth.

Be aware that the compiler can warn when you do something stupid. It can also generate code to explode at runtime when you do something stupid. Just never rely on it. He cannot detect everything, and you yourself must know the rules.

+1
source share

All Articles