C / C + + Programming Notes: undefined behavior in C language and C + +, and its advantages and disadvantages analysis

Time:2021-4-3

Consider the following C / C + + programs and try to guess what the output is?

Example 1:

C / C + + Programming Notes: undefined behavior in C language and C + +, and its advantages and disadvantages analysis

 

Example 2:

C / C + + Programming Notes: undefined behavior in C language and C + +, and its advantages and disadvantages analysis

 

Example 3:

C / C + + Programming Notes: undefined behavior in C language and C + +, and its advantages and disadvantages analysis

 

Example 4:

C / C + + Programming Notes: undefined behavior in C language and C + +, and its advantages and disadvantages analysis

 

Example 5:

C / C + + Programming Notes: undefined behavior in C language and C + +, and its advantages and disadvantages analysis

 

Example 6:

C / C + + Programming Notes: undefined behavior in C language and C + +, and its advantages and disadvantages analysis

 

Example 7:

C / C + + Programming Notes: undefined behavior in C language and C + +, and its advantages and disadvantages analysis

 

The output of all the above programs is unpredictable (or uncertain). The compiler, which implements the C / C + + standard, is free to do anything because the C and C + + standards do not define them.

Languages like Java find errors immediately, but in a few cases, languages like C and C + + continue to execute code in a silent but error prone manner, which can lead to unpredictable results. The program may crash due to any type of error message, or it may unknowingly damage the data, which is a serious problem.

Understand the importance of undefined behavior

If users begin to learn in the C / C + + environment and are not clear about the concept of undefined behavior, many problems may arise in the future. For example, it may be difficult to trace the root directory when debugging other people’s code. Undefined error.

Undefined behavior

OfRisks and disadvantagesProgrammers sometimes rely onUndefined behaviorThis may cause problems when changing / upgrading the compiler. For example, the last program produces 72 as output in most compilers, but it’s not a good idea to implement software based on this assumption.

Undefined behavior can also lead to security vulnerabilities, especially due to unchecked array bounds (leading to buffer overflow attacks).

Advantages of undefined behavior

C and C + + have undefined behavior because it allows the compiler to avoid a lot of checking. It is assumed that a group of code with high performance array does not need to pay attention to the boundary, which avoids the need for complex optimization process to check such conditions outside the loop. Tight binding loops can speed up programs from 30% to 50% when they take advantage of the undefined nature of symbolic overflow that C compilers usually offer.

We also have another advantage, because it allows us to store the value of a variable in the processor register and operate in a larger time than the variable in the source code. It also helps to do surround checking, followed by compile time checking, which cannot be done without a better understanding of undefined behavior in the C / C + + compiler.

Hope to help you~

In addition, if you want to better improve your programming ability, learn C language c + + programming! Overtaking on the curve, one step faster! I may be able to help you here~

C language c + + programming learning circle,QQ group 1090842465Click to enterWeChat official account: C language programming learning base

Share (source code, project video, project notes, basic introductory course)

Welcome to change and learn programming partners, use more information to learn and grow faster than their own thinking Oh!

Programming learning book sharing:

Programming learning video sharing: