看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《CindyLinz (尹兒)》之銘言: : int main(){ : data_t data; : int i; : for(i=0; i<1024; ++i) : data.heap[i] = i; : printf("%d\n", data.stack[-1]); /* 1023 */ : printf("%d\n", data.stack[-10]); /* 1014 */ : printf("%d\n", data.stack[-1000]); /* 24 */ : printf("%d\n", data.stack[-1024]); /* 0 */ : return 0; : } 不好意思但這個應該是未定義行為。C99+TC1/2/3 draft (n1256) 和 C++03 說... C99+TC1/2/3 6.5.2.1/2 ... The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). ... C++03 5.2.1[expr.sub]/1 ... The expression E1[E2] is identical (by definition) to *((E1)+(E2)). ... 所以讓我們來看指標運算: C99+TC1/2/3 6.5.6/8 When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. ... If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. C++03 5.7[expr.add]/5 When an expression that has integral type is added to or subtracted from a pointer, the result has the type of the pointer operand. ... If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. 另外就是 struct 裡面和後面可能會亂塞東西,只有開頭不能塞。 C99+TC1/2/3 6.7.2.1/13 ... There may be unnamed padding within a structure object, but not at its beginning. C99+TC1/2/3 6.7.2.1/15 There may be unnamed padding at the end of a structure or union. C++03 9.2[class.mem]/17 ... [Note: There might therefore be unnamed padding within a POD-struct object, but not at its beginning, as necessary to achieve appropriate alignment. ] 有一些關於 bit-field 的小例外就懶得貼了。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.30.39
james732:這個板讀過標準書的人怎麼越來越多了...我也要趕個流行嗎 06/26 20:56
johnhmj:當我一看到陣列的flag用負值…就有一種很危險的感覺~ 06/26 21:36
angleevil:其實要不要看標準書是個人精進選擇,累積一定的技術和 06/26 21:40
angleevil:英文能力去看那些,可能都還要想很久才會理解制定的精神 06/26 21:41
angleevil:所以我寧願看別人根據例子狀況,在po文章就好. 06/26 21:41
angleevil:我承認能力還不到看文件的水準. 06/26 21:42
loveme00835:我記得之前就po過阿.. 06/26 21:46
Favonia:er... 不好意思很多之前的文章都沒看 orz 06/26 23:02
CindyLinz:一萬八千篇要怎麼看啊... XDrz 06/26 23:10
firejox:應該不會很遠.... 06/27 00:09
firejox:#1DgV76tB 06/27 00:11
firejox:不過這次換拿C++規格書了XD 06/27 00:29
Favonia:喔對還有陣列大小,不過那個我覺得標準在精神上很矛盾 orz 06/27 00:36
(原來發表另一篇,版主建議合併到同一篇 xD) 感謝 firejox 大大提醒,忘了還有陣列大小的問題!不過根據 C++03 標準,一個符合 標準的實作需要產生「陣列大小為零」錯誤訊息,所以其實也不用太擔心,除非... 程式本 身違反了標有未定義行為的規則!這個程式就是一個例子。 C++03 1.4[intro.compliance]/1 The set of diagnosable rules consists of all syntactic and semantic rules in this International Standard except for those rules containing an explicit notation that "no diagnostic is required" or which are described as resulting in "undefined behavior." C++03 1.4[intro.compliance]/2 ... -- If a program contains a violation of any diagnosable rule, a conforming implementation shall issue at least one diagnostic message, except that -- If a program contains a violation of a rule for which no diagnostic is required, this International Standard places no requirement on imple- mentations with respect to that program. 所謂精神上矛盾,是因為 C++03 允許 new int[0] 卻禁止 int A[0]; 我想這應該只是 為了讓 new T[n]; 比較好寫而已... 個人非常討厭這種不一致的設計。待會回一篇有關 C 的。 // 以下是原推文 → loveme00835:動態跟靜態有差, 陣列是0該怎麼處理? 反之要0個元素卻 06/27 01:42 → loveme00835:可以回傳一個合法位址來免掉存取違規, 雖然可以用 06/27 01:44 → loveme00835:nullptr或是例外處理來做掉, 但是速度上有差, 這也是 06/27 01:48 → loveme00835:不預設支援exception的原因之一 06/27 01:48 個人覺得一個好的設計是要讓不正常的行為早點發現,並讓程式當掉。一個空的陣列幾 乎什麼都不能做,很難講不是程式本身的邏輯有問題。 // 補上這段澄清 啊不過我只有主張標準對於空陣列的態度應該要統一 xD 如果要允許空陣列,那就到處 都允許,並好好定好語意吧! // 下面這兩個推文請看下篇。
Favonia:不過那篇裡面有技術細節錯了... 06/27 00:37
loveme00835:@_@ ? 06/27 00:37
※ 編輯: Favonia 來自: 140.112.30.39 (07/23 22:26)