了解瓦尔格林德的输出

Understanding Valgrind's output

本文关键字:输出 林德 了解      更新时间:2023-10-16

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

==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]时,我也得到了这个结果,这意味着它本身就是gData的位置,对吧?

RegPoly:

std::vector<double> gData;
std::vector<double>::iterator gDataIterator;
int gCurrentRank;
bool _isZeroPoly; // Inherited from the interface

信任Valgrind。它几乎总是显示出存在的问题。

以下是您阅读信息的方式:

RegPoly::getCurrentCoefficient() const (RegPoly.cpp:59)试图从不应该读取的内存位置读取,因为它不属于您的应用程序(未分配、非堆栈等)。

这个无效地址刚好超出了RegPoly复制构造函数中使用的new运算符分配的空间(您可以看到创建该调用的整个回溯)。

我没有看到您的复制构造函数复制gDataIterator

问题在RegPoly.cpp:59上,而不是在DemoPol.cpp:50中。

声明您已经分配了16个字节,但它正在尝试读取超出您分配的8个字节。

请参见:4.2。Memcheck错误消息的解释

可能您正试图访问一个无效的迭代器。const迭代器只是意味着您不能使用此迭代器来修改容器中的值。

与无效迭代器有关的简要说明。