Invalid read of size 8
I have written a class for a sequence of doubles in c++ using a
dynamically allocated array.. When running the program, it completes
successfully, but valgrind is finding an error. I receive Invalid read of
size 8 when my resize function is called.
void sequence::resize(size_type new_capacity){
if (new_capacity == capacity){
return;
}else {
if (new_capacity < capacity)
used = new_capacity;
capacity = new_capacity;
value_type* new_vals;
new_vals = new value_type[capacity];
for (int i=0;i<used;i++){
new_vals[i] = data[i];
}
cout<<endl;
data = new_vals;
}
}
Here is the error I am receiving in my test program:
==1919== Invalid read of size 8
==1919== at 0x400DB3: main_savitch_4::sequence::resize(unsigned long)
(sequence2.cxx:44)
==1919== by 0x401091: main_savitch_4::sequence::attach(double const&)
(sequence2.cxx:95)
==1919== by 0x403232: test5() (sequence_exam2.cxx:538)
==1919== by 0x40414E: run_a_test(int, char const*, int (*)(), int)
(sequence_exam2.cxx:744)
==1919== by 0x404321: main (sequence_exam2.cxx:775)
==1919== Address 0x5a1ae50 is 0 bytes after a block of size 240 alloc'd
==1919== at 0x4C2C037: operator new[](unsigned long) (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1919== by 0x400C33: main_savitch_4::sequence::sequence(unsigned long)
(sequence2.cxx:17)
==1919== by 0x4030AC: test5() (sequence_exam2.cxx:520)
==1919== by 0x40414E: run_a_test(int, char const*, int (*)(), int)
(sequence_exam2.cxx:744)
==1919== by 0x404321: main (sequence_exam2.cxx:775)
==1919==
I have tried running valgrind with --leak-check=full and
--read-var-info=yes and cannot determine why I am getting this error. Line
45 of resize is the one that reads: new_vals[i] = data[i];
Thanks!!!
No comments:
Post a Comment