我正在为大学写一个项目,一切都完成了,通过了所有的测试,运行良好,但瓦尔格林告诉我:

==8059== Invalid read of size 8
==8059==    at 0x406E4E: RegPoly::getCurrentCoefficient() const (RegPoly.cpp:59)
==8059==    by 0x403368: MyPoly::operator+(MyPoly const&) const (MyPoly.cpp:281)
==8059==    by 0x403A6D: MyPoly::operator+=(MyPoly const&) (MyPoly.cpp:354)
==8059==    by 0x401E20: main (DemoPoly.cpp:50)
==8059==  Address 0x5953f50 is 0 bytes after a block of size 16 alloc'd
==8059==    at 0x4C27297: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8059==    by 0x4060CD: __gnu_cxx::new_allocator<double>::allocate(unsigned long, void const*) (new_allocator.h:92)
==8059==    by 0x405934: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned long) (in /a/fr-05/vol/home/stud/lablabla/CppLab/Ex3/DemoPoly)
==8059==    by 0x407355: double* std::vector<double, std::allocator<double> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > > >(unsigned long, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >) (stl_vector.h:1052)
==8059==    by 0x40700E: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (vector.tcc:167)
==8059==    by 0x406C5D: RegPoly::RegPoly(RegPoly const&) (RegPoly.cpp:19)
==8059==    by 0x4031DB: MyPoly::operator=(MyPoly const&) (MyPoly.cpp:249)
==8059==    by 0x403B58: MyPoly::operator*=(MyPoly const&) (MyPoly.cpp:364)
==8059==    by 0x401DB6: main (DemoPoly.cpp:44)

台词:
44-p3 *= p2; // both are MyPoly objects
50-p3 += p1;
我有点明白,这意味着我试图从一个已经改变了的记忆中阅读但我不明白为什么。
我可以张贴相关的代码,只是,我不确定哪一部分,因为它会使问题混乱。我可以粘贴相关部分,告诉我你需要什么。
谢谢!
编辑:
代码如下:
MyPoly& MyPoly::operator *=(const MyPoly& rhs)
{
    *this = *this * rhs; // 364
    return *this;
}

===================

case PolyInterface::REG:
{
    RegPoly *tempReg = dynamic_cast<RegPoly*>(rhs.p_PolyBody); // rhs is an interface, hence the dynamic cast
    if (tempReg != NULL)
    {
        p_PolyBody = new RegPoly(*tempReg);  // 249. p_PolyBody is a pointer stored in MyPoly. points to RegPoly object
    }
    break;
}

===================

RegPoly::RegPoly(RegPoly const& other)
{
    gCurrentRank = 0;
    gData = other.gData; // 19. gData is a vector<double>
    _isZeroPoly = other._isZeroPoly;
}

===================

double RegPoly::getCurrentCoefficient() const
{
    return *gDataIterator; // 59. vector<double>::const_iterator
}

===================

newPolyValues.push_back(
                p_PolyBody->getCurrentCoefficient() + rhs.p_PolyBody->getCurrentCoefficient());
// 281.

编辑:
getCurrentCoefficient()返回gData[gCurrentRank]
意思是它和格达的位置有关,对吧?
雷戈里:
std::vector<double> gData;
std::vector<double>::iterator gDataIterator;
int gCurrentRank;
bool _isZeroPoly; // Inherited from the interface

最佳答案

信任Valrnd,它几乎总是显示出存在的问题。
以下是您阅读邮件的方式:
RegPoly::getCurrentCoefficient() const (RegPoly.cpp:59)试图从不应该读取的内存位置读取,因为它不属于您的应用程序(未分配、非堆栈等)。这个无效地址刚好超出了新operaator分配的空间,这个operaator用于RegPoly复制构造函数(您可以看到创建该调用的整个回溯)。
如果这是不够的帮助,请张贴相关代码。
更新:
我看不到你的复制构造函数复制了gDataIterator(顺便问一下,当你复制时,为什么要将gCurrentRank初始化为0?).

关于c++ - 了解valgrind的输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12308927/

10-10 14:19