Valgrind 告诉我我的代码中有一个错误,但我找不到它......这是一个代码片段:
22 int main(int argc, char *argv[]){
...
//argv[1] contains the name of a file
int length=atoi(argv[2]);
map<string, double> Map;
char* Key;
Key = new char [length+1];
...
我也使用过Key[length]='\0'
,但这似乎不会影响其余代码。
现在我用从文件中获取的条目填充地图(其中包含键和值行的列表。键的维度总是低于长度。
while( file_stream >> Key >> Value){
Map[Key]=Value;
....
}
在这一点上,我打电话给:
cout << Key << " has value ";
159 cout << Map[Key] << endl;
该程序编译和执行得很好,但是 Valgrind 给出了很多这种类型的错误:
==6921== Conditional jump or move depends on uninitialised value(s)
==6921== at 0x56274A0: __printf_fp (printf_fp.c:404)
==6921== by 0x562396A: vfprintf (vfprintf.c:1622)
==6921== by 0x5648C81: vsnprintf (vsnprintf.c:120)
==6921== by 0x4EB64AE: ??? (in /usr/lib/libstdc++.so.6.0.13)
==6921== by 0x4EB9002: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_float<double>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, char, double) const (in /usr/lib/libstdc++.so.6.0.13)
==6921== by 0x4EB9328: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, double) const (in /usr/lib/libstdc++.so.6.0.13)
==6921== by 0x4ECCC9E: std::ostream& std::ostream::_M_insert<double>(double) (in /usr/lib/libstdc++.so.6.0.13)
==6921== by 0x40366E: main (MyProgram.cpp:159)
==6921== Uninitialised value was created by a stack allocation
==6921== at 0x401C61: main (MyProgram.cpp:22)
你有什么想法吗?我宁愿不发布所有长代码(请不要为此责备)
谢谢!!