Re: [問題] cpp 考題請教
※ 引述《m8403051 (大吉嶺紅茶)》之銘言:
: 開發平台(Platform): Linux CentOS 7
: 編譯器: gcc version 4.4.7 20120313 (Red Hat 4.4.7-23)
: 預期的正確結果(Expected Output):180
: 錯誤結果(Wrong Output):215
: 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
: https://ideone.com/e.js/nn576o
: #include <iostream>
: using namespace std;
: int main() {
: int a=6, b=7;
: a *= a-- * --b;
: cout << a << endl;
: return 0;
: }
: 補充說明(Supplement):
: 使用 ideone.com 跑出來是正確的結果 180, 但是使用 CentOS 7 g++ 跑出來卻是 215
: 請教各位先進這是哪邊沒注意到?
GCC 4.4 算比較舊的編譯器, 預設會以 C++98 來編譯程式碼, 敘述
的求值 (evaluation) 就看標準定義的 sequence point 有幾個以
及插在什麼地方. 我手邊只有 ISO/IEC 14882:2003 所以可能和更
早的資料有點出入, C++98 和原文有關的描述主要如下:
[expr] 5.4
https://i.imgur.com/9WB2V5H.png
剛好範例裡的 i = ++i + 1 因為 sequence point 在這個 statem-
ant 裡只有一個, 即完整敘述的結束, 多次更改 i 的值所以行為是
unspecified; 但是這在 C++11 (N3242) 裡定義得更精確了:
[intro.execution] 1.9/15
https://i.imgur.com/b8zsTdZ.png
因為有了 sequenced before 的觀念, 在等號右邊使用一次 prefix
++ 的行為被明確定義了:
The value computations of the operands of an operator
are sequenced before the value computation of the result
of the operator
和 C++98 相比, 在等號右邊敘述計算 + 之前多了一個 sequence
point, 所以 prefix ++ 的問題解了, 再來 postfix ++ 要到
C++17 (N4659) 才經由 [P0145] 確立執行結果:
[P0145R3] [Core] Refining Expression Evaluation Order
for Idiomatic C++
https://bit.ly/2z9o0vM
[intro.execution] 4.6/17
https://i.imgur.com/9fmE9yJ.png
C++17 直接保證了等號左右邊的求值順序 (先右後左). 總結一下兩
種敘述在各年代標準底下的行為 (結果):
┌─────┬──────┬──────┐
│ Standard │i = --i + 1 │i = i-- + 1 │
├─────┼──────┼──────┤
│ C++98 │ UB │ UB │
├─────┼──────┼──────┤
│ C++11 │ i │ UB │
├─────┼──────┼──────┤
│ C++17 │ i │ i + 1 │
└─────┴──────┴──────┘
再回到原來的程式碼:
int a = 6, b = 7;
a *= ((a--) * (--b));
--b 基本上可以直接代換成 6, 變成只有 a 的運算:
a *= ((a--) * 6);
那麼問題來了, 如果 *= 左邊和右邊的相對求值順序沒有定下來,
此題就沒標準答案 (與賦值無關), 但如果以 C++17 或更新的標準
來看: 等號右邊 (a--) 的 side effect 會先被產生, 然後才是等
號左邊拿 a 的值來做運算, 這時候就形同於以下的賦值敘述:
a *= (6 * 6); // a becomes 5 after a--
=> a = 5 * (6 * 6); // a = 180
--
P1389R0: Guidelines for Teaching C++ to Beginners
https://bit.ly/2GvDWKb
SG20 Education and Recommended Videos for Teaching C++
https://www.cjdb.com.au/sg20-and-videos
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.193.76.216 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1587389865.A.CA4.html
→
04/21 08:58,
4年前
, 1F
04/21 08:58, 1F
→
04/21 08:59,
4年前
, 2F
04/21 08:59, 2F
感謝, 修正好了 :)
推
04/21 09:52,
4年前
, 3F
04/21 09:52, 3F
→
04/21 09:52,
4年前
, 4F
04/21 09:52, 4F
更新後記得要用 -std=c++17 來指定標準版本
※ 編輯: poyenc (61.216.75.43 臺灣), 04/21/2020 10:20:15
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章