简体   繁体   中英

How to get more useful warnings from Visual Studio C compiler?

I am using Visual Studio 2015 C compiler and I find it lacks of warnings.
For example this code compile without warning or error (with -Wall option):

#include<stdlib.h>

int main(void)
{
    int i = 2;

    free(&i);
// The compiler should tell me "You are trying to free something not on the heap".

    return 0;
}

There is a lot of things that causes crash or/and undefined behavior that are not detected with -W4 or -Wall , is there other options I can use to be warned about these mistakes?
I know I could use another compiler, but I heard the one that comes with Visual Studio is better when you are targeting Windows platform.

Using the compiler to generate warnings, is, as you correctly point out not a complete detection of undefined behavior.

Turning on optimizations strangely improves the compilers reporting. It spots variables which have not been set up (value tracking) and some unneeded values.

To go further than the compiler, then static analysis tools such as coverity and pc-lint do stronger analysis. However, these products are also falsy, and will highlight some issues which are correct, that they misunderstand, and still miss some undefined behavior.

Particularly if you are cross-platform compiling, I would have a more relaxed attitude to the compiler's warnings (clean with gcc AND VS is really difficult, and may damage your code) and treat your static analysis tool as a code-reviewer. When it suggests a failure, treat it with respect, but assume it could have mis-understood the code.

I had a copy of pc-lint hanging around and got this...

--- Module:   file.c (C)
        _
    free(&i);
file.c(7) : Warning 424: Inappropriate deallocation (free) for 'auto' data

---

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM