Reputation: 980
Valgrind tells me that there is an error in my code, but I am not able to find it... here is a code snippet:
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];
...
I also used Key[length]='\0'
but this does't seem to affect the rest of the code.
now I fill in the map with entries taken from a file (which contains a list of keys and values rows. The dimension of Key is always lower of length.
while( file_stream >> Key >> Value){
Map[Key]=Value;
....
}
at this point, I call:
cout << Key << " has value ";
159 cout << Map[Key] << endl;
The program is compiled and executed nicely, but Valgrind give lots of errors of this type:
==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)
Do you have any idea any clue? I would prefer to not post all the long code (please don't blame for this)
Thanks!!
Upvotes: 1
Views: 2795
Reputation: 153830
The stack trace implies that you are formatting a double
. There is no way an uninitialized double
can be obtained from Map[Key]
unless you explicitly stuck an uninitialized double
into it (even the Key
doesn't exist in Map
, it would be initialized using double()
). This could point to a piece of interesting code within the implementation of __printf_fp()
: I'd verify with a simple test program whether this report, indeed, can be reproduced using, e.g.:
#include <iostream>
int main() {
std::cout << 3.14;
}
... and, if so, I'd just ignore the report from valgrind
. During a really calm afternoon it may be reaosnable to dig through the implementation of __printf_fp()
to find where it does something questionable and determine whether it actually matters (I'd not gamble on that ever happening to me, though: I'd rather implement floating point formatting myself which woudl, however, probably take a lot more than one afternoon as it is distinctly non-trivial).
Upvotes: 1