標籤:
C 語言中副作用(side effect):是指對資料對象或者檔案的修改。例如,語句 var = 99; 的副作用是把 var 的值修改成 99。對錶達式求值也可能產生副作用,例如,對錶達式求se = 100;
求值所產生的副作用就是 se 的值被修改成 100。
序列點(sequence point):是指程式運行中的一個特殊的時間點,在該點之前的所有副作用已經結束,並且後續的副作用還沒發生。C 語句結束標誌——分號(;)是序列點。標準規定,在兩個序列點之間,一個對象所儲存的值最多隻能被修改一次。
C 語句中由賦值、自增或者自減等引起的副作用在分號之前必須結束。我們以後會說到一些包含序列點的運算子。任何完整運算式(full expression)運算結束的那個時間點也是序列點。所謂完整運算式,就是說這個運算式不是子運算式。而所謂的子運算式,則是指運算式中的運算式。例如:f = ++e % 3 這整個運算式就是一個完整運算式。這個運算式中的 ++e、3 和 ++e % 3 都是它的子運算式。
有了序列點的概念,我們下面來分析一下一個很常見的錯誤:
int x = 1, y;y = x++ + x++;
這裡 y = x++ + x++ 是完整運算式,而 x++ 是它的子運算式。這個完整運算式運算結束的那一點是一個序列點,int x = 1, y; 中的 ; 也是一個序列點。也就是說,x++ + x++ 位於兩個序列點之間。標準規定,在兩個序列點之間,一個對象所儲存的值最多隻能被修改一次。但是我們清楚可以看到,上面這個例子中,x 的值在兩個序列點之間被修改了兩次。這顯然是錯誤的!這段代碼在不同的編譯器上編譯可能會導致 y 的值有所不同。比較常見的結果是 y 的值最後被修改為 2 或者 3。在此,我不打算就這個問題作更深入的分析,各位只要記住這是錯誤的,別這麼用就可以了。有興趣的話,可以看看以下列出的相關資料。
C 語言標準對副作用和序列點的定義如下:
Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.
翻譯如下:
訪問易變對象,修改對象或檔案,或者調用包含這些操作的函數都是副作用,它們都會改變執行環境的狀態。計算運算式也會引起副作用。執行序列中某些特定的點被稱為序列點。在序列點上,該點之前所有運算的副作用都應該結束,並且後繼運算的副作用還沒發生。
順序點有:
1. The point of calling a function, after evaluating its arguments.
2. The end of the first operand of the && operator.
3. The end of the first operand of the || operator.
4. The end of the first operand of the ?: conditional operator.
5. The end of the each operand of the comma operator.
6. Completing the evaluation of a full expression. They are the following:
7. Evaluating the initializer of an auto object.
8. The expression in an ‘ordinary’ statement—an expression followed by semicolon.
9. The controlling expressions in do , while , if , switch or for statements.
10. The other two expressions in a for statement.
11. The expression in a return statement.
編譯時間可以加上“-Wsequence-point ”讓編譯器幫我們檢查可能的關於檢查點的錯誤。
* test_sequence_point.c * gcc -Wsequence-point test_sequence_point.c */ #i nclude int main() { int i = 12; i = i--; printf("the i is %d/n", i); return 0; }
gcc -Wsequence-point test_sequence_point.c test_sequence_point.c: In function `main‘: test_sequence_point.c:10: warning: operation on `i‘ may be undefined
轉載於:http://blog.csdn.net/bnufq/article/details/8953465
C語言 side effect和sequence point