開發平台(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):