Re: [問題] 龐大的二維矩陣建立
※ 引述《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
→
06/26 20:56, , 1F
06/26 20:56, 1F
→
06/26 21:36, , 2F
06/26 21:36, 2F
→
06/26 21:40, , 3F
06/26 21:40, 3F
→
06/26 21:41, , 4F
06/26 21:41, 4F
→
06/26 21:41, , 5F
06/26 21:41, 5F
→
06/26 21:42, , 6F
06/26 21:42, 6F
→
06/26 21:46, , 7F
06/26 21:46, 7F
→
06/26 23:02, , 8F
06/26 23:02, 8F
→
06/26 23:10, , 9F
06/26 23:10, 9F
→
06/27 00:09, , 10F
06/27 00:09, 10F
→
06/27 00:11, , 11F
06/27 00:11, 11F
→
06/27 00:29, , 12F
06/27 00:29, 12F
→
06/27 00:36, , 13F
06/27 00:36, 13F
(原來發表另一篇,版主建議合併到同一篇 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 如果要允許空陣列,那就到處
都允許,並好好定好語意吧!
// 下面這兩個推文請看下篇。
→
06/27 00:37, , 14F
06/27 00:37, 14F
→
06/27 00:37, , 15F
06/27 00:37, 15F
※ 編輯: Favonia 來自: 140.112.30.39 (07/23 22:26)
討論串 (同標題文章)