[問題] HEAP CORRUPTION DETECTED

看板C_and_CPP作者 (無良記者)時間11年前 (2013/01/15 14:09), 編輯推噓1(105)
留言6則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 2010 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) iostream, iomanip 問題(Question): 大家好,我是一個剛開始自學 C++的程式新手 兩天前發了一篇關於二維陣列動態配置的問題(#1GyeLVK-),獲得板友的熱心回覆 參考了板友們的建議,改了一些地方,這次在for 迴圈終於能把值及平均 但在印出第二行的值之後,卻出現了錯誤訊息:http://i.imgur.com/RCOxN.png
主要的問題訊息是「heap corruption detected」 google了之後,應該是在new的時候出了問題,但不知道要如何解決 請問有板友有類似的經驗或解決之道嗎?感謝 餵入的資料(Input): 假設 y, 1, y, 2, y, 3, y, 4, y, 5, y, 6, y, 7, y, 8, y, 9, y, 10, n 預期的正確結果(Expected Output): Enter a number('y' to continue or 'n' to exit): y 1 Enter a number('y' to continue or 'n' to exit): y 2 Enter a number('y' to continue or 'n' to exit): y 3 Enter a number('y' to continue or 'n' to exit): y 4 Enter a number('y' to continue or 'n' to exit): y 5 Enter a number('y' to continue or 'n' to exit): y 6 Enter a number('y' to continue or 'n' to exit): y 7 Enter a number('y' to continue or 'n' to exit): y 8 Enter a number('y' to continue or 'n' to exit): y 9 Enter a number('y' to continue or 'n' to exit): y 10 Enter a number('y' to continue or 'n' to exit): n 1 2 3 4 5 3 6 7 8 9 10 8 錯誤結果(Wrong Output): 印出第二行之後,就出現http://i.imgur.com/RCOxN.png
程式碼(Code):(請善用置底文網頁, 記得排版) #include <iostream> #include <iomanip> using std::cin; using std::cout; using std::endl; using std::setw; int main() { const int MAX = 5; //固定陣列第二維大小為5 int (*pstr)[MAX] = new int[1][MAX]; //宣告一個動態二維陣列 int *ptr = &pstr[0][0]; char letter = 'y'; //判斷是否要繼續輸入數字的變數 int count = 0; //計算輸入的所有字數 double value = 0.0, total = 0.0; //計算每五個的和及總和 cout << endl << "Enter a number('y' to continue or 'n' to exit): "; cin >> letter; cout << endl; //輸入 'y'以接著輸入數字,輸入 'n'停止並作計算 /**************這裡的while迴圈是要把輸入的數字都存到動態二維陣列裡***********/ /***************************並計算一共輸入幾個數字***************************/ while(letter == 'y') { cin >> *ptr //把數字輸入到陣列裡 ptr++; //陣列指標移到下一個位置 count ++; //每輸入一個數字,總數就加1 cout << endl << "Enter a number('y' to continue or 'n' to exit): "; cin >> letter; cout << endl; } /************這裡的for迴圈是要把陣列裡的數字一列(5個)一列印出來************/ /****************每印一列就算這列的總和及平均,並把平均印出來****************/ for(int i = 0; i < count/5; i++) //這理想用陣列的列數做控制 { for(int j = 0; j < 5; j++) //這是為了把每列的數字都拿出 來 { cout << setw(5) << pstr[i][j]; value += pstr[i][j]; } cout << setw(5) << value / 5 << endl; //印出該列的平均值 total += value; //加到總和上 value = 0.0; //歸零 } delete [] pstr; return 0; } 補充說明(Supplement): 無 --

07/30 21:39,
☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑
07/30 21:39

07/30 21:44,
綠紅奶西珍檸情高綠紅奶西珍檸情高綠紅奶西珍檸情高綠
07/30 21:44

07/30 21:44,
茶茶茶瓜珠檬人山茶茶茶瓜珠檬人山茶茶茶瓜珠檬人山
07/30 21:44

07/30 21:44,
   枝奶茶茶茶   枝奶茶茶茶   枝奶茶茶茶
07/30 21:44
-- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.27.23.168

01/15 15:13, , 1F
為什麼不用置頂文貼程式碼呢
01/15 15:13, 1F
我覺得這樣很清楚就是......而且版上也不少人直接貼程式碼出來 如果板友覺得這樣很不清楚的話,那我以後用置底的網頁貼程式碼吧0.0

01/15 22:07, , 2F
問題在於你只向系統要了 5 個 int,卻用了 10 個 int
01/15 22:07, 2F
這不是會從free store多配給程式用嗎0.0? 為什麼還是會有問題呢? 還是我哪裡觀念不對?

01/15 22:14, , 3F
我只看到你配了第一次 int[1][MAX]
01/15 22:14, 3F

01/15 22:16, , 4F
你應該加上"判斷是否超過,並且需要重新配置"的 code
01/15 22:16, 4F
原來如此......我還以為只要配一次就好了0.0 我再想想要如何加上去......

01/15 22:22, , 5F
http://codepad.org/UrMZNuCg 我想你還是貼會比較好
01/15 22:22, 5F

01/15 22:22, , 6F
別人會比較方便看,BBS比較沒那麼方便看程式碼
01/15 22:22, 6F
了解~ ※ 編輯: o07608 來自: 114.27.23.168 (01/15 22:24)
文章代碼(AID): #1GzFAPpD (C_and_CPP)