[C language] a + = b and a = a + b really is exactly equivalent to it? a [i] = i ++ in the end, right?

▎a + = b and a = a + b really is exactly equivalent to it?

In the C language, I believe that a + = b and a = a + b we have written, that you have not thought about both what is not exactly equivalent to it?

In fact, the difficulty of this problem in the data type of a and b, to two cases:

1, for the same type of a, b for the

The results of the implementation of two formulas really no difference. But from the viewpoint of the compiler, a + = b; when performed efficiently.

2, for different types of a, b for the

 When two different types of variables during the operation, we often say that the type of conversion. Here, please keep in mind that: during operation, the type of transition to the low-precision high precision type.

In the above code, if a + =, b is converted directly into char, if using a + b, a first converted into int, the last assigned when converted to char.

Thus, in general, a = a + b; and a + = b; Equivalence is not any time, depending on the situation should be divided because the data type is converted here, we want to use later for both the caution.

 

▎a [i] = i ++ in the end, right?

Programming sometimes encounter some ambiguous expressions, such as a [i] = i ++. So a [i] = i ++ in the end right?

Consider first the following code:

For this expression a [i] = i ++, subexpression i ++ has a side effect, it will change the value of i, since i will be referenced in the same expression, and therefore this will lead to undefined behavior . Since this reference can not be determined (the left side of equation a [i]) is the value of the new or old value.

Different compilers have different interpretation understood when such acts, such as the following three compiler (dev c ++, codeblocks, vs2019) to said code have different interpretations.

Screenshot operation can be observed from above into the same piece of code, and the execution result dev c ++ codeblocks are the same, but they are not the same and vs2019.

For such behavior, although some believe that behavior in the literature of such expressions is uncertain, but the c standard has strong statement that it is undefined.

Other examples of undefined behavior include access an array beyond its borders, null pointer dereference, after the end of life access to an object or writing allegedly intelligent expression like i ++ + ++ i.

Undefined behavior there are two less dangerous brother, undefined behavior and implementation-defined behavior.

So implementation-defined behavior, undefined behavior, the difference between these three undefined behavior where is it?

First of all these three cases represent the c language standard does not explicitly require the use of a specific structure or its field program must complete things. This loose definition of c language is traditional, but the way this provision is well thought out, this approach allows the definition of:

1, select certain configuration can generate efficient code in accordance with the "hardware to complete manner."

2, ignoring some difficult precise definition, and the boundary structure may be a good writing program of no practical use.

 

▎ for the definition of these three "standard does not precisely defined behavior" are as follows:

1, implementation-defined behavior

Certain aspects of the machine's operation and is described in this International Standard is implementation-defined (e.g., sizeof (int)). These constitute the parameters of the abstract machine. Each embodiment includes a file should be characterized and behavior in these aspects.

2, undefined behavior

And certain other aspects of the operation of the abstract machine described in this International Standard is unknown (e.g., the order of evaluation function parameters). Where possible case, C international standard language defines a set of permissible behavior. These define the non-deterministic aspects of the abstract machine.

3, undefined behavior

Anything can happen, this standard does not have any requirements, procedures may fail to compile, runtime error (crash directly or generate erroneous results) or fortunately as programmers want.

Since the standard compiler does not make any requirements, then the compiler can make any possible behavior. Endure the idea is not defined in the program is extremely dangerous, even undefined undefined behavior than you think.

If you want to write portable code, then the above three actions are needed to avoid. We therefore preferable to avoid a [i] = i ++ C language such undefined written when writing the code.

       For those who love programming, the! I have started to learn a programming zero-based exchange club ( group ), a group of learning together answered with a small partner is very important, as well as learning video files, you are welcome beginners and advanced in little friends!

Published 520 original articles · won praise 132 · views 80000 +

Guess you like

Origin blog.csdn.net/HUYA69/article/details/105291837