【发布时间】:2012-01-29 21:51:44
【问题描述】:
由于某种原因,以下 C++ 代码会导致分段错误:
#include <sstream>
#include <vector>
using namespace std;
string charToString(char c)
{
stringstream ss;
string s;
ss << c;
ss >> s;
return s;
}
int main()
{
vector<string> stringTable;
for(int c = 0; c < 256; ++c){
string s = charToString(c);
stringTable[c] = s;
}
}
valgrind在线报错Invalid read of size 8
stringTable[c] = s;
但我看不出这条线有什么问题。那么这段代码有什么问题呢?
【问题讨论】:
-
你的向量是空的。你可以使用 push_back() 来填充它。
标签: c++ string vector segmentation-fault