Warning: Smoking is harmful to health; Programmer: I don’t care about warnings, only mistakes

 

Wow, the functions are finally realized, it's over, so happy,,,

  

 

Ok? I see your code? , Why is there so much warnning?

  

what? What are you afraid of warnning? There is nothing wrong with it. Submit it now, so as not to get a headache.

  

That’s not good, two warnings may cause an error

 

Come on, I will treat certain warnings as errors through the compilation options . Are you trying to compile?

Ok? What fuck, why are there dozens of errors?

  

Warnning is just a minor error. To write robust code, you must pay attention to these.

  

Okay, I continue to stay on the road to fix the bug. . . . . .

  

  

Regarding warnning as nothingness, is it because you don’t know where the bug comes from?

In development, we often encounter warnings due to various reasons. Although they do not affect specific functions and no errors occur, they always seem to be annoying, especially for developers with obsessive-compulsive disorder like me.

Too many warnings will affect the discovery of real problems and hide our sight.

All warnings that appear are justified, figure it out.

Warnings are often due to potential threats to memory scheduling, etc. In most cases, there will be no errors, and once an error occurs, it is catastrophic.

Usually, our computer has a blue screen or a cell phone is stuck.

So when you encounter a warning, understand it, this is the best time to develop your skills

Isn't it a bit too cruel to treat warnning as an error?

The warning is not necessarily an error, right, it can be ignored when it is confirmed that it will not affect the running result.

Sometimes, the warning is because the writing is not standardized, changing a way of writing is also an exercise for yourself.

When the code is submitted to the testing department, black-box and white-box testing can also be passed, right?

But in the development process, not all warnings need to be heeded, after all, the project progress is there.

If you explain the reason, or the compiler warning, skip it for the time being, there is no way.

warnning < ? < error

Are there any warnings that are indeed errors, but the compiler is not so strict? Or is there any warnning that is really okay and doesn't need to spend time to find out. Wouldn't it improve efficiency?

It is safer than "Ignore all warnings", and looser and more accurate than turning on "Treat all warnings as errors" , so the following is very important!

Personal summary should be regarded as an error warning

Variables are used without initialization

After the function is called, there is no guarantee how the used stack frame space will be used in the future (whether optimization is enabled by the compiler or the stack frame layout structure has an impact), luckily it will not work.

Assign int pointer and int to each other

Although the value of a pointer (an address) can be understood as an int (actually an unsigned int), consider this situation: it int a=*pis written as int a=pan error.

The format string and actual parameter type in statements such as printf do not match

E.g

printf("%s%d, szDebugString, ulGwId);

Your ulGwId is an unsigned long type, but the output format you choose for it is "%d".

Compare two variables of type unsigned int and int

A signed number may be converted to an unsigned number before the comparison and the result will be wrong, so I won't say more about this.

Function is used without declaration

Looking at books about c programming, you can know that before a function is called, a declaration is required. This is to tell the compiler the type of the return value of the function, and the type and number of parameters the function accepts.

For example, a warning will appear if the function is not declared. Go and see the result for yourself

#include <stdio.h>
 
int main(void)
{
 int c;
 
     c = sun();
 printf("%d",c);
 return 0;
 
}
int  sun()
{
 return 3;
}

Incompatible pointer types

E.g

#include <stdio.h>
int main()
{
  int array[3][4];
  int **p = array;
}

Then we can change it like this and there is no problem.

#include <stdio.h>
int main()
{
  int array[3][4];
  //int **p = array;
 int (*p)[4] = array;
}

The function should have a return value but no return value

Reason for warning: such a warning appears, it may be that you wrote a

unsigned long FuncA()
{
  if()
  {
    return ulValue;
  }
  if()
  {
    return ulValue;
  }
}

Such a function, you may not enter in the two if statements, at this time, before exiting the function, you have no value to return. So make sure that the function has a return value in any case.

A shadow variable is used

The inner scope redeclares/defines the variable with the same name as the outer scope. Give an example to illustrate the harm of shadow variables:

void set_value(int* val) 
{
  double r = 0.0;
  if(isRandom) {
      double r = this->generateRandomNumber();
  }
  *val = r;
}

After the above code runs, the value of val is always 0 and cannot be changed to a random value.

The function returns the address of a local variable

In C, under normal circumstances, we can only return a value from a function. But in many cases, we need to return multiple values ​​from the function. At this time, using arrays or pointers can accomplish this task well.

Here is an example. This program uses an integer array as a parameter, and returns the sum and product of the array elements to the calling function. code show as below:

#include <stdio.h> 
#include <conio.h> 
int* Pool(int array[],int size)  
{  
   int *x;  
   int i=0;  
   int a[2]={0,1};  
   for(i=0;i<size;i++)  
   {  
      a[0]+=array[i];      //存储数组元素值的和  
      a[1]*=array[i];      //存储数组元素值的积  
   }  
   //将数组的基地址赋值给整型指针  
   x=&a[0];  
   //返回整个数组  
   return x;  
}  
 
int main()  
{  
   int a[]={1,2,3,4};  
   int *c; c = Pool(a,4);  
   printf("Sum = %d\nProduct = %d\n",c[0],c[1]);  
   getch();  
   return 0;  
} 

In this way, use arrays and pointers to return multiple values ​​from C functions. In many cases, you will find this technique useful, but in C language you must never return the address of a local variable in a function.

In the C language, a typical mistake is to use a pointer to a local variable as the return value of a function.

Since the array is a local variable, its array space has been invalidated when the function returns, that is, the pointer uses a meaningless address space, so there will be no return value.

There are many warnings

In fact, there are many more. I will not list them one by one here. There are endless bugs and countless hairs. Welcome to leave a message to add!

At last

Warnings are still very important. In the final analysis, what we are doing is to completely remove any threats that damage the code, so that we can write robust code, and then we can benefit future generations after we leave!

Guess you like

Origin blog.csdn.net/u012846795/article/details/108191707