
CppCat is a simple static code analyzer for finding errors in C / C ++ programs. We started issuing free academic licenses to everyone (students, teachers, and so on). For more popularization of CppCat among students, I decided to write this error note, which can be found in the labs found on the site
Pastebin.com .
A little bit about CppCat
CppCat is a static code analyzer that integrates into the Visual Studio environment and allows you to find many typos and other errors at the coding stage. The analyzer can start automatically after compilation and check the code just written. The analyzer supports C, C ++, C ++ / CLI, C ++ / CX.
How to get a free CppCat license is described in the article:
Free CppCat for students . I want to add that we give a license not only to students, but also graduate students, teachers and so on.
Many are frustrated that CppCat does not integrate into the free Visual Studio Express environment. Unfortunately, we can not do anything about it. Express versions of Visual Studio do not support plugins. However, it does not matter. I would like to remind that students have access to
Microsoft DreamSpark and can get access to Visual Studio Professional.
')
Student enticement
In the beginning I wanted to write something in the spirit:
Even a student can benefit from static analysis. Why look for a long and painful mistake in your program if CppCat can tell about it? Thus, it is possible not only to find the error more quickly, but also to learn the details on how to do it. Documentation for CppCat explains in detail each diagnosis and gives various examples of errors, suggests how to fix them.
Then he decided that somehow it was stretched. Well, what are the students serious errors? It is also possible to work out a loop of 10 iterations. It will even be useful. Therefore, I will restate the recommendation to use CppCat as follows:
The employer will appreciate not only your ability to program and write clever algorithms. It is equally important to be able to use basic tools.
The price is worth the algorithm, if it is lost due to the fact that you do not know what a version control system is and the maximum that you did - made a copy of the source codes on a USB flash drive.
Therefore, it is important to study not only languages ​​and development environments, but also supporting tools.
I do not dare to make a list of the most important tools. However, it is important to know at least one version control system, why WinMerge may be needed, what a profiler is, how to make a distribution kit, and so on.
One of the technologies that is well known and can be mentioned in the summary is static code analysis. CppCat is great for exploring static analysis methodologies. This will be a good plus for your knowledge and will hint to the employer that you have heard something about the quality of the code.
And now for what we are here gathered
The boring part is over. I think you can guess that now we will look for errors in laboratory work.
To say that the code is often bad, this time I will not. And so it is clear that the laboratory code contains the darkness of various errors. Therefore, I set the task not to find as many errors as possible. It is not interesting. I tried to highlight some patterns of errors that occur most often in students. True, while I was able to clearly notice the predominance of errors of only three types. But about it a little lower.
You may have a question: where did I get the laboratory work? I answer.
There is a site
Pastebin.com , where developers can conveniently exchange code snippets. Students actively use this site. Almost all the code that has a C ++ tag is some kind of lab or some part of it.
We have written a program that monitors the site Pastebin.com and downloads from there fresh files marked as “C ++ code”. Having collected over two thousand files, I made a project from them in Visual Studio and checked it. More than half, of course, could not be verified. In many files, only code snippets, text that is not a comment is entered, some libraries are missing, and so on. However, I did not set out to check as much as possible the code published on Pastebin.com. What I checked was enough for this article. The collection of files continues and maybe I will write something else later on this topic.
Typical mistakes of students studying C ++
I looked through not too many errors in the lab. So while I can select only 3 patterns.
I will not give all examples of errors in a row. They are of the same type and uninteresting. I will confine myself to just a few examples. But believe me, if I say that such a mistake is common, it really is.
PS Many of the published examples were time limited and are no longer available. Therefore, I will not give links to deleted pages.
Pattern 1. Third place in popularity. Confusion in the same conditions
Many programming tasks involve checking many conditions. And, realizing them, it is easy to get confused or sealed. Illustrative example:
int main() { int n,a,b,c; cin >> n; for(int i=0;i<n;i++) { cin >> a >> b >> c; if((a % 2==0 && b % 2 ==0 && c % 2!=0)|| (a % 2==0 && b % 2!=0 && c % 2==0)|| (a % 2!=0 && b % 2==0 && c % 2==0)|| (a % 2!=0 && b % 2 !=0 && c % 2==0)|| (a % 2==0 && b % 2!=0 && c % 2!=0)|| <<<--- (a % 2==0 && b % 2!=0 && c % 2!=0)) <<<--- { cout << "1"; } else cout << "2"; } cout << endl; return 0; }
CppCat warning: V501 There are identical sub-expressions '(a% 2 == 0 && b% 2! = 0 && c% 2! = 0)' || operator. jtzrihcg.cpp 14
It was necessary to check the values ​​of the three variables entered cunningly. Most likely, the code was copied and not everywhere was correctly corrected. As a result, the penultimate and last line in the condition are the same.
Another
example :
int main() { .... } else if(gesucht < geraten) { puts("Ein bisschen zu klein"); } else if (gesucht < geraten) { puts("Ein bisschen zu gross"); } .... }
V517 The use of if (A) {...} else if (A) {...} 'pattern was detected. There is a possibility of logical error presence. Check lines: 41, 43. wrgkuuzr.cpp 40
A check is performed twice (gesucht <geraten), but different lines should be displayed.
By the way, in both examples the error is in the last line. Again we met the "
last line effect ".
Pattern 2. Second place in popularity. Array overrun by 1 element
The fact that the elements of arrays are numbered from zero in C ++ is a great difficulty when studying it. Those. it seems easy to understand, but learning how not to go beyond the boundary of an array is very difficult. If you need 10 elements of an array, then you just want to write A [10].
Example :
int main() { .... int rodnecs[10]; .... VelPol1 = rodnecs[1] + rodnecs[3] + rodnecs[5] + rodnecs[8] + rodnecs[10]; .... }
CppCat warning: V557 Array overrun is possible. The '10' index is pointing beyond array bound. 0z3x9b3i.cpp 38
More :
void main() { .... double pop[3][3]; .... for (int i = 0; i<3; i++) { calc_y[i] = F(pop[i][1], pop[i][2], pop[i][3], x[i]); } .... }
CppCat warning: V557 Array overrun is possible. The '3' index is pointing beyond array bound. 1uj9v9xs.cpp 48
Many wrong comparisons in terms of cycles:
int main() { int i,pinakas[20],temp,temp2,max,min,sum=0; for (i=1;i<=20;i++) { pinakas[i]=rand(); ...... }
CppCat warning: V557 Array overrun is possible. The value of 'i' index could reach 20. 287ep6c0.cpp 20
A
lot :
int main() { const int arraySize = 10; int a[arraySize]; int key,index,to_do = arraySize - 1; bool did_swap = true; srand(time(NULL)); for (int i = 0; i <= arraySize; i++) {
CppCat warning: V557 Array overrun is possible. The value of 'i' index could reach 10. wgk1lx3u.cpp 18
The remaining errors are similar to those given above, so let's finish.
Pattern 3. First place in popularity. Uninitialized variables
ABOUT! I seem to understand why, if you don’t ask, one of the most frequent and dangerous mistakes in C / C ++ programming is what people call “uninitialized variables”. But, while analyzing
projects with the help of PVS-Studio, I rarely meet this error.
Why? Apparently, when learning a language, everyone is very busy getting bumps on it. Thus, programmers almost never make such mistakes. But the memories remain. And when asked what to fear, “uninitialized variables” will often answer.
There is a very
simple :
int main() { .... int n,k=0, liczba=n, i=1; .... }
CppCat warning: V614 Uninitialized variable 'n' used. 1hvefw6r.cpp 92
You can incorrectly work with the list:
void erase(List * Lista){ List* pom; pom->next = Lista->next; Lista->next= pom; delete pom; }
CppCat warning: V614 Uninitialized pointer 'pom' used. 6gpsgjuy.cpp 54
You can
make a loop with a random number of iterations:
void main() { int i,n; imie* ime[20]; string nazwa; string kobieta="Kobiece imina: "; wpr_dane(); for (i = 1; i < n; i++) { .... }
CppCat warning: V614 Uninitialized variable 'n' used. 8kns8hyn.cpp 63
You can first use and then enter the value of a variable:
int main() { int n1; int n2; std::vector<int> vec1(n1); std::vector<int> vec2(n2); std::cin >> n1; for (int i = 0; i < n1; i++) { std::cin >> vec1[i]; } std::cin >> n2; for (int j = 0; j < n2; j++) { std::cin >> vec2[j]; } .... }
CppCat Warnings:
- V614 Uninitialized variable 'n1' used. 9r9zdkp6.cpp 25
- V614 Uninitialized variable 'n2' used. 9r9zdkp6.cpp 26
Next, give examples, I think it makes no sense But, believe me, students shoot their legs with uninitialized variables in a variety of ways.
Other errors
Of course, in the laboratory I saw a lot of other various errors. I cannot highlight the same large groups of errors as described above. Although I can name several other notable groups: incorrect calculation of the size of arrays, semicolon, early termination of a cycle, incorrect work with arrays, WTF.
Incorrect calculation of array size
Many beginners have a hard time understanding that in C / C ++, a pointer and an array are different entities. As a result, there is often a code like
this :
int arrayLen(int p[]) { return(sizeof(p)/sizeof(*p)); }
CppCat warning: V511 The sizeof () operator returns the sizeof (p) 'expression. seprcjvw.cpp 147
True, the function arrayLen () is not used anywhere. Apparently, due to the fact that it does not work. :)
Another
example :
bool compare_mas(int * mas, int * mas2){
CppCat Warnings:
- V514 Dividing sizeof a pointer 'sizeof (mas)' by another value. There is a possibility of logical error presence. 0mxbjwbg.cpp 2
- V514 Dividing sizeof a pointer 'sizeof (mas2)' by another value. There is a possibility of logical error presence. 0mxbjwbg.cpp 3
Not there is a semicolon ';'
These errors are not as common as I expected. They are, but I can’t call it a common mistake in laboratory work.
Typical
example :
vector sum(vector m[],int N){ vector sum,tmp; for (int i=0;i<N;i++); { tmp.a=m[i].a; tmp.b=m[i].b; tmp.c=m[i].c; sum.a+=tmp.a; sum.b+=tmp.b; sum.c+=tmp.c; } return sum.a,sum.b,sum.c; }
CppCat warning: V529 Odd semicolon ';' after 'for' operator. knadcqde.cpp 122
Early Interrupt Cycle
There are a number of
examples where the cycle is accidentally interrupted ahead of time:
int main() { .... for (long long j = sled.size()-1; j > i; j --) { sled[j] = '0'; des = 1; break; } .... }
CppCat warning: V612 An unconditional 'break' within a loop. XHPquVXs.cpp 31
Incorrect array handling
In several laboratories, work with arrays in the Pascal style was met. That is, a comma is used, which, although compiled, works, of course, incorrectly:
void build_maze(){
CppCat warning: V520 The comma operator ',' in array index expression '[aktualny.x - 1, aktualny.y]'. qqxjufye.cpp 125
Or they
forget that the memory for the returned arrays should be allocated in a special way:
int *mul3(int *a) { int mem = 0; int b[1001]; for (int i = 100; i >= 0; i--) { int x = a[i] * 3 + mem; mem = x / 10; b[i] = x % 10; } return b; }
CppCat warning: V558 Function returns the pointer to a temporary local object: b. hqvgtwvr.cpp 89
WTF
There are code snippets that I, except as WTF, cannot name. Maybe someone asked classmate to explain where the error in the program. Although, rather, this laboratory is just for studying the overflow of arrays. Unfortunately, I do not know what is written in the comments.
Let me
give you one whole
example :
#include <iostream> using namespace std; int main() { int a[10]; for(int i=0; i<50; i++) cout << a[i] << endl; //ovoj loop ili kje krashne ili kje ti nedefinirani vrednost //(ne mora da bidat 0) //ako namesto 50 stavis 500000, skoro sigurno kje krashne int b[10]; for(int i=0; i<50; i++) { b[i] = i; cout << b[i] << endl; } //ovoj loop nekogas kje raboti, nekogas ne. problemot so //out-of-bounds index errori e sto nekogas rabotat kako //sto treba, pa greskata tesko se naogja }
What else is not included article
A lot of things are not included! For example, the printf () functions are misused. But it is so banal that you don’t even want to write about it.
However, quite exotic types of errors
were also
encountered :
void zmienne1() { .... int a,b,c,d; cin >> a >> b >> c >> d; if(a == b == c == d) .... }
CppCat warning: V709 Suspicious comparison found: 'a == b == c'. Remember that 'a == b == c' is not e qual to 'a == b && b == c'. b5lt64hj.cpp 284
Too from the rare (if, of course, not to look at compiler warnings):
const long AVG_PSYCHO = 0.8; const long AVG_GRAD = 1.2;
CppCat Warnings:
- V674 The '0.8' literal of the 'double' type is assigned. Consider inspecting the '= 0.8' expression. 2k2bmnpz.cpp 21
- V674 The '1.2' literal of the 'double' type is assigned. Consider inspecting the '= 1.2' expression. 2k2bmnpz.cpp 22
However, it is necessary to finish. I hope I entertained readers and seduced someone to try CppCat.
Why we do not plan to do any online analyzer
I foresee the question: "Why do not you make some kind of system for online code checking?". For example, there is some form where you can insert the code and click the "check" button. Or, since you are monitoring the site pastebin.com, so why not spread the results of the test somewhere?
I am sure that we do not need to do this. There are three reasons for this, so please do not start a discussion on this topic.
The reasons:
- Neither we nor the user need this. It will add us work. And the user will not receive anything new. He can simply download and install PVS-Studio or CppCat and carry out all the experiments that he wishes. The demo version will be more than enough. Often, the “insert and check code” form is done by those who simply cannot download the trial version. We can. Moreover, it does not have any functional limitations. Someone else can say that he does not have Windows, but he wanted to try something. But since he does not have Windows, he is still not our user.
- Such a system greatly distorts the assessment of the capabilities of the static analyzer. An article on this topic: Myths about static analysis. The fifth myth - you can create a small program to evaluate the tool . We want people to test the analyzer on their real projects, and not on synthetic examples.
- As I said, we do not want to check synthetic examples. And to check the project entirely difficult from the infrastructure point of view. Read more about this in the interview . Briefly: you have to make a complex system, where you need to download source files, libraries, set up build parameters, and so on. Otherwise, a full analysis is impossible. It turns out that it is easier to download the analyzer, install and check.
Conclusion
Dear students and teachers, we will be glad to see you among our users. I wish students to become qualified specialists and subsequently persuade their team to purchase PVS-Studio for teamwork.
Additional links:
- PVS-Studio for Visual C ++ .
- Alternative to PVS-Studio for $ 250 .
- Comparison of the capabilities of the static code analyzers PVS-Studio and CppCat .
- Free CppCat for students .
Unfortunately, we no longer develop or support the CppCat project. You can read about the reasons here . |