Analysis of common problems in C Programming

Source: Internet
Author: User
Document directory
  • 1.2.4 multiple judgment problems of if Statements
Analysis of common problems in C Programming
The general syntax and programming skills in C language have been mentioned in many books. The following describes some very important issues that are rarely mentioned in other books. These are some of the lessons learned by the author in the programming process. It can be said that professional programmers encounter these problems every day during programming.
1.2.1 parameter verification problems

In C-language functions, function parameters are generally verified, but in some cases, they are not verified in the function, and the caller checks them externally, under what circumstances should I perform the verification in the function, and under what circumstances should I not perform the verification in the function? The following principles are for your reference.
1) for functions that need to be called in a large loop, parameter verification is not required within the function.
For example, the List_EnumNext function void * List_EnumNext (LIST * pList) is used to traverse the linked LIST one by one ).
In the traverse functions of the linked list, do you want to verify the validity of pList parameters? The answer is no. Why? Because the traversal functions of a linked list are usually used in a loop, for example, if a linked list has 10 000 nodes, it is required to traverse 10 000 times one by one. If the preceding function verifies the pList parameter, the traversal process of the entire linked list is verified 10 000 times. It is better for the caller to verify the parameter once before calling the function. Therefore, function parameters that may be called frequently and can only be verified once in the external verification do not need to be verified inside the function.
2) The underlying function calls are frequently performed and generally not verified.
3) parameters must be verified for functions with low call frequency.
4) if the execution time of a function with a large overhead is negligible in parameter verification compared to the entire function, it is generally best to verify the function parameters.
5) function parameters must be verified to greatly improve the stability of the software.
1.2.2 return Statement

In functions, using the return statement may also be worth exploring. Some people think that the exit of the function should be minimized, so the use of the return statement should be reduced; especially when there is a resource allocation operation in the function, in each subsequent return statement, corresponding release operations are required. During programming, omissions may easily occur and resource leakage may occur. But we also know that if we want to reduce the return statement, there will be other problems. First, in many cases, it is more difficult to program the return statement. Secondly, sometimes the return statement is reduced and the number of layers nested in braces is increased, increasing the length of the code line, it is easy to read the code because it contains more than 80 characters.
So under what circumstances can return statements be reduced and under what circumstances can return statements be reduced? The following principles are for your reference.
1) Check the parameters. If the verification fails, the return Statement is generally used. This makes the program logic clear and easy to read, and reduces the number of layers nested in braces.
2) There must be different return statements for different internal errors of the function; otherwise, the real cause of the error cannot be distinguished for external callers.
3) For similar errors within the function, try to use only one return statement, that is, try not to let the two return statements return the same return value.
1.2.3 while loop and for Loop Problems

1. Differences between the two loops in the construction of an infinite loop
When an infinite loop is constructed using the while clause, the while (TRUE) clause is generally used to construct an infinite loop. When the for clause is used to construct an infinite loop, the for (;) clause is used to construct an infinite loop. The difference between the two endless loops is that the conditions in the while loop are regarded as expressions. Therefore, when the while loop is used to construct an endless loop, TRUE is actually regarded as a TRUE expression forever, this situation is prone to confusion. Some tool software such as PC-Lint will think that an error has occurred. Therefore, it is best to use for (;) to construct an endless loop.
2. Differences between the two types of loops in normal Loops
When looping an array, it is generally easier to use the for loop if the variable increases only after the loop is processed; if you want to increase the number of loop variables in the Process of loop processing, it is more convenient to use the while loop. Also, when using the for loop statement, if the loop condition inside is very long, you can consider using the while loop to make the code layout look better.
1.2.4 multiple judgment problems of if Statements

When verifying parameters, you often need to verify several parameters of the function. Each parameter is verified using a separate if statement, or are multiple statements placed in one if statement used for logic or operation verification? Use an instance to describe it.
Example 1-1 parameter verification.
TABLE * CreateTable1 (int nRow, int nCol)
{
If (nRow> MAX_ROWS)
{
Return NULL;
}
If (nCol> = MAX_COLS)
{
Return NULL;
}
......
}
TABLE * CreateTable2 (int nRow, int nCol)
{
If (nRow> MAX_ROWS | nCol> = MAX_COLS)
{
Return NULL;
}
......
}
Which of the following statements is better in the CreateTable1 () and CreateTable2 () functions? The second method is better. Why? In the second write method, a return statement is missing. After compilation, the code to be executed is smaller than the first write method, and the memory consumed during execution is naturally less, the performance in other aspects is the same. Interested readers can decompile the above two sections of code for analysis.

1.2.5 goto statement Problems

After structured programming technology, goto statements are treated as a typical representative of the destruction of structured programs. It can be said that in the era of structured program design, goto statements are just like the flood of beasts, so programmers are afraid to avoid them; however, in some examples of Microsoft programs, the goto statement is often used to handle errors. When an error occurs, The goto interface to a label to exit the function for resource release and other operations. So can the goto statement be used only for error handling, but not elsewhere? The following principles about the use of the goto statement can be used for your reference.
1) The goto statement can only be used in the same function, but not from one function to another.
2) When a goto statement is used in the same function, the start point of goto should be the end of a small function in the function, the target label of goto should start with another small function in the function.
3) It is not allowed to jump from a complex execution state location goto another location. For example, jumping out from multiple nested loops is not allowed.

1.2.6 switch... Case and if... Differences in else if efficiency

Many books on C language have said that switch... Case is more efficient than if... Else if is efficient. In fact, this conclusion is not completely correct. The key is to look at the actual code. In a few cases, if... If else if is properly written, it is more efficient than switch... Case.
Example 1-2 if... Else if statement test example.
Void TestIfElse ()
{
Unsigned int x;
Srand (1);/* initialize the random number generator */
X = rand () % 100;
If (x = 0)
{
......
}
Else if (x = 1)
{
......
}
Else
{
......
}
}
X is 0 ~ A random number in the range of 100. when x is not 0 or 1, The else branch is executed. Therefore, the above function has a 98% probability of executing the else branch, the probability of executing an if branch and an else if branch is 1%. When executing the else branch, you must first execute the if judgment statement, and then execute the else if judgment statement before executing the content in the else branch. Therefore, the else branch must be judged twice. If we change the execution of the else branch to the if branch as follows, the efficiency will be greatly improved.
Void TestIfElse ()
{
Unsigned int x;
Srand (1);/* initialize the random number generator */
X = rand () % 100;
If (x> 1)
{
......
}
Else if (x = 0)
{
......
}
Else
{
......
}
}
If the above Code is implemented using switch... case, the efficiency is not as high as the above function. You can try it out.
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.