看板 Programming 關於我們 聯絡資訊
小弟寫一個類別 有一個成員是指標 class A { A(); ~A(); int* array; }; 在建構子中 給這個指標分配空間 A::A() { array = new int [size]; } 在解構子中刪除這個記憶體 A::~A() { if(array) { delete array; array = 0; } } 這樣子寫之後,會遇到兩個問題, 1.debug版程式在結束時delete array出現 HEAP CORRUPTION DETECTED: CRT detected that the application wrote to memory after the end of heap buffer 2.release版本結束時不會跳出error但執行時有時會當機(視size而定), 但是在偵錯模式下執行又沒有問題 後來我上網查一下之後 改成array = new int [size+1] 以上兩個問題神奇的都解決了,但是不知道背後的道理總是怕怕的 所以想請教各位這個問題出現的緣由? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.71.113
purpose:heap 會貪污,就是程式有 bug 124.8.133.82 07/05 14:12
purpose:http://tinyurl.com/7tky5ww 124.8.133.82 07/05 14:14
saxontai:delete [] array; 210.80.75.2 07/05 21:37