Valgrind告訴我在我的代碼中存在錯誤,但我無法找到它... 這裏是一段代碼:「未初始化的值是由堆棧分配創建的」在C++ std :: map
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'但是然後使用動態分配的'char * Key'而不是'string Key'? –
us2012
「你有什麼想法爲什麼我這樣做的錯誤」 - *爲什麼*你做了這個錯誤?不是一個線索。很難進入這個思維過程。 **發佈*真實*,*可編譯*代碼**。 – WhozCraig
@WhozCraig你是對的,我編輯了這個問題。 – altroware