[問題] 塞ADDR進VECTOR為何會CRASH

看板C_and_CPP (C/C++)作者 (reverse(wasefo);)時間16年前 (2010/01/17 23:16), 編輯推噓0(007)
留言7則, 2人參與, 最新討論串1/1
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 ) ( 未必需要依照此格式,文章條理清楚即可 ) 遇到的問題: (題意請描述清楚) 我有個class mat大小280 想要塞入 vector<mat*> heap 可是我在塞第三個addr of object時 就CRASH了 而且每次都是第三個就GG 是用固定SAMPLE vector有限大小嗎!@?!?! mat* child=0; ... child = new mat(selected->childrenInstance[drt], mat::id_counter++, mat::reverse_direction(drt), selected); 希望得到的正確結果: 可以塞得進去 程式跑出來的錯誤結果: CRASH 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) dev-c++ on win7 有問題的code: (請善用置底文標色功能) vector<mat*> heap; heap.push_back(input); heap_push(heap.begin(),heap.end(),mat::compare); ... mat* child=0; mat* selected=heap[0]; ... ... child = new mat(selected->childrenInstance[drt], mat::id_counter++, mat::reverse_direction(drt), selected); cout << " new ok " << endl; // 這行OK heap.push_back(child); cout << " push ok " << endl; // 這行沒跑出來就CRASH了 所以我想應該是在PUSH時GG 補充說明: 是因為我沒 delete 嗎!?!? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.116.13.191

01/18 02:10, , 1F
=_=?怎麼不用wxDev-C++?
01/18 02:10, 1F

01/18 02:15, , 2F
物件指標最好用list<mat*>~
01/18 02:15, 2F

01/18 14:38, , 3F
不要塞指標 除非你要多型 就算要多型 也找個 SmartPtr用
01/18 14:38, 3F

01/18 14:39, , 4F
或考慮改用 vector<mat> 吧 你可先resize 然後直接取出
01/18 14:39, 4F

01/18 14:40, , 5F
最後一個element 做你要做的運算
01/18 14:40, 5F

01/18 14:41, , 6F
或者直接用 vec.push_back(*new mat(...)); 期待compiler
01/18 14:41, 6F

01/18 14:41, , 7F
optimization 做得好 :p
01/18 14:41, 7F
文章代碼(AID): #1BKod6OM (C_and_CPP)
文章代碼(AID): #1BKod6OM (C_and_CPP)