Rus

Online Examples (C, C++)

Thanks to the Compiler Explorer project, programmers can now quickly prepare synthetic examples of C and C++ code and see what results PVS-Studio will produce when run on them.

Here is an example of code with two bugs in it, which demonstrates PVS-Studio's data flow analysis abilities.

First, there is a typo that leads to storing the addresses of two allocated buffers twice in a row into the 'a' variable. This results in losing one of the pointers, and so the analyzer warns us about a memory leak. Another effect of that typo is null pointer dereferencing in the 'use' function.

Second, the memory was allocated using the 'new' operator but freed using the 'free' function.

The analyzer reports all these defects by issuing a few warnings:

More examples

We have prepared a few more examples demonstrating other diagnostic abilities of the PVS-Studio analyzer:

You can modify our examples or compose your own from scratch.

Applications of the feature

There are a few things you can do with PVS-Studio in online mode on Compiler Explorer:

What you shouldn't try to do in this mode:

A note for students and teachers

Compiler Explorer comes in handy when you need to do a short lab assignment on programming. You can write a code snippet, make sure it compiles, run it, and see the results. You can then generate a link to share with your teacher for assessment.

In addition to this, there is now the ability to check the code using PVS-Studio which will help to get started with static analysis. We know that students and teachers are using PVS-Studio in free mode quite a lot. The new feature makes experimenting with PVS-Studio especially easy for those who use Compiler Explorer to teach and learn C and C++.

References