I am writing an application which is supposed to implement polynomials. It involves dynamic memory allocation, therefore I check it with valgrind. Once I noticed the following problem and I have no idea how to fix it
When using valgrind I receive
Line 28 in testpoly contains following
cout << "Value of polynomial P1 at point 3.14: " << val << endl;
where double val = P1(3.14);
Problem vanishes when i transform operator () function in th e following way
I am not very experienced in C++ programming and valgrind therefore I will appreciate any kind of help.
Martin
Code:
double poly::operator() (const double& value)
{
double tmp;
for(unsigned i=0;i<=degree;i++)
tmp+=values[i]*value;
return tmp;
}
ostream & operator << (ostream & o, const poly & p)
{
for (unsigned i=0;i<=p.degree;i++){
o << p[i];
o << "(X^"<<i<<")";
if(i+1<=p.degree)
if((p.values[i+1]>=0)&&(i<p.degree))
o << "+";
}
return o;
}
Code:
==6045== Conditional jump or move depends on uninitialised value(s)
==6045== at 0x1BA819CD: _IO_file_xsputn (in /lib/tls/libc-2.3.5.so)
==6045== by 0x1BA77561: fwrite (in /lib/tls/libc-2.3.5.so)
==6045== by 0x1B992496: (within /usr/lib/libstdc++.so.6.0.5)
==6045== by 0x1B991E79: 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.5)
==6045== by 0x1B9922CB: 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.5)
==6045== by 0x1B995CC5: std::ostream::operator<<(double) (in /usr/lib/libstdc++.so.6.0.5)
==6045== by 0x8048B61: main (testpoly.cpp:28)
==6045==
==6045== Syscall param write(buf) points to uninitialised byte(s)
==6045== at 0x1BADDA63: write (in /lib/tls/libc-2.3.5.so)
==6045== by 0x1BA8187E: _IO_file_write (in /lib/tls/libc-2.3.5.so)
==6045== by 0x1BA802A4: (within /lib/tls/libc-2.3.5.so)
==6045== by 0x1BA803BE: _IO_do_write (in /lib/tls/libc-2.3.5.so)
==6045== by 0x1BA80C9D: _IO_file_overflow (in /lib/tls/libc-2.3.5.so)
==6045== by 0x1BA82413: __overflow (in /lib/tls/libc-2.3.5.so)
==6045== by 0x1BA7DD58: putc (in /lib/tls/libc-2.3.5.so)
==6045== by 0x1B99281B: (within /usr/lib/libstdc++.so.6.0.5)
==6045== by 0x1B996517: std::ostream::put(char) (in /usr/lib/libstdc++.so.6.0.5)
==6045== by 0x1B996621: std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&) (in /usr/lib/libstdc++.so.6.0.5)
==6045== by 0x1B99388E: std::ostream::operator<<(std::ostream& (*)(std::ostream&)) (in /usr/lib/libstdc++.so.6.0.5)
==6045== by 0x8048B72: main (testpoly.cpp:28)
==6045== Address 0x1B903026 is not stack'd, malloc'd or (recently) free'd
cout << "Value of polynomial P1 at point 3.14: " << val << endl;
where double val = P1(3.14);
Problem vanishes when i transform operator () function in th e following way
Code:
double poly::operator() (const double& value)
{
double tmp;
for(unsigned i=0;i<=degree;i++)
tmp+=values[i]*value;
return 0 /*or some other constant*/;
}
Martin