工作時C++分段錯誤我正在與3D向量和一切工作完美。當我添加並使用流文件時出現了分段錯誤。 我完全不理解這個問題。 下面的代碼不起作用:與<vector>和<fstream>
#include <iostream>
#include <vector>
#include <fstream>
std::vector < std::vector < std::vector <float> > > hand;
int main(){
//First Part
std::ofstream file;
file.open("test.csv");
file << "Hello World!";
file.close();
//Second Part
hand.reserve(20);
for (int i=0; i<hand.capacity(); i++){
hand[i].reserve(4);
}
return 0;
}
如果你對此有何評論代碼將很好地工作的組成部分之一。當您想要同時使用它們時,出現分段錯誤。
同樣重要的是要注意到,該代碼可以工作,如果不是:
hand.reserve(20);
我們用下面8號:
hand.reserve(7); //or lower
我的問題是:爲什麼不工作的代碼當我同時使用它們?我能做些什麼來解決它?你有關於這個特殊情況的解釋嗎?
我必須在一個更大的代碼中實現它,所以最好知道根本原因並避免在接下來的情況下。
你有沒有調試應用程序,並尋找任何0值? – 2013-04-24 08:04:31