[問題] vector.clear() seg fault
開發平台(Platform): (Ex: Win10, Linux, ...)
Linux
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
g++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
我最近發現某些情況下用vector.clear()會seg fault
例如下面的code
我在for loop外面declare我的2D vector
還有我要當成row的1D vector
裡面存int
每個interation我要重新建立這個DS還有裡面的資料
所以用vector.clear();
不過我發現這行好像會導致seg fault
後來我把1D vector的declaration移到for loop裡面
一切就正常了
覺得很疑惑
想請問有沒有人知道可能的原因
為何不能這樣子用clear()?
餵入的資料(Input):
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
vector< vector<int> > temp_vec;
vector<int> temp_vec_row(length, 0);
for()
{
temp_vec.clear();
//vector<int> temp_vector_row(length, 0);
for()
temp_vec.push_back(temp_vec_row);
// Do something to fill the 2D vector
}
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 128.61.13.105
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1478121493.A.85C.html
※ 編輯: fishlinghu (128.61.13.105), 11/03/2016 05:47:45
→
11/03 08:05, , 1F
11/03 08:05, 1F
→
11/03 09:44, , 2F
11/03 09:44, 2F
→
11/03 10:38, , 3F
11/03 10:38, 3F
→
11/03 10:39, , 4F
11/03 10:39, 4F
→
11/03 11:24, , 5F
11/03 11:24, 5F
→
11/03 11:24, , 6F
11/03 11:24, 6F
推
11/03 11:53, , 7F
11/03 11:53, 7F
推
11/03 13:52, , 8F
11/03 13:52, 8F
→
11/03 13:52, , 9F
11/03 13:52, 9F
→
11/03 13:53, , 10F
11/03 13:53, 10F
推
11/03 20:25, , 11F
11/03 20:25, 11F
→
11/04 04:30, , 12F
11/04 04:30, 12F