【发布时间】:2025-12-25 05:00:06
【问题描述】:
Valgrind 告诉我我的代码中有错误,但我找不到它... 这是一个代码sn-p:
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)
你有什么线索吗?我宁愿不发布所有长代码(请不要为此责备)
谢谢!!
【问题讨论】:
-
为什么你有一个
map<string,double>,然后使用动态分配的char* Key而不是string Key? -
“你知道我为什么会犯这个错误吗” - 为什么你会犯这个错误?一点头绪都没有。很难进入那个思考过程。 发布真实,可编译代码。
-
@WhozCraig 你是对的,我编辑了这个问题。
-
@us2012 感谢您的提示,我将更正代码,看看会发生什么。
-
请在此处和/或 ideone.com 发布 sscce。