瓦尔格林德内存泄漏与动态数组C++

Valgrind memory leak with dynamic arrays C++

本文关键字:动态 数组 C++ 泄漏 内存 林德      更新时间:2023-10-16

我已经尝试了几种方法来解决这个问题,但似乎无法弄清楚。Valgrind 指出我的调整大小方法存在内存泄漏,我觉得我可能错过了一些简单的东西。

.h 文件

private:
  int* pArray;     // stores math expression
  int currentSize; // size of array

。.cpp

void Prog::resize(int newSize)
{
  cout << "Address of polynomial:ttt" << &pArray << endl;
  int* temp = new int[newSize] {0};
  cout << "Address of temp:ttt" << &temp << endl;
  copy(temp, pArray);
  delete[] pArray;
  pArray = temp;
  cout << "Address of pArray after swap:t" << &pArray << endl;
  temp = nullptr;
  currentSize = newSize;
}
void Prog::copy(int* to, int* from)
  {
    for (int i = 0; i < currentSize; i++)
      to[i] = from[i];
  }

我添加了 cout 以查看地址发生了什么,因为我认为在将 pArray 交换为 temp 后,它会打印出哪个 temp 位置的地址,但它似乎保留了其原始位置。这是应该发生的事情吗?

我尝试创建交换方法,使用它时问题仍然相同。

void Prog::swap(int*& to, int*& from) 
{
  int* temp = to;
  to = from;
  from = temp;
} 

这是我运行程序和瓦尔格林德时的输出。

程序截图

Address of pArray:                  0000006741EFF218
Address of temp:                    0000006741EFEEE8
Address of pArray after swap:       0000006741EFF218
D = +50x^20000 +15x^11 +5x^10 -12x^7 -4x^6 +30x^5 +4x^4 -2x^3 +50

瓦尔格林德

==22696== 24 bytes in 1 blocks are definitely lost in loss record 1 of 12
==22696==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22696==    by 0x40230A: Prog::resize(int) (Prog.cpp:332)
==22696==    by 0x401DD7: Prog::operator=(Prog const&) (Prog.cpp:171)
==22696==    by 0x400DEE: main (lab1.cpp:36)
==22696==
==22696== 36 bytes in 1 blocks are definitely lost in loss record 2 of 12
==22696==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22696==    by 0x40230A: Prog::resize(int) (Prog.cpp:332)
==22696==    by 0x401DD7: Prog::operator=(Prog const&) (Prog.cpp:171)
==22696==    by 0x400DD5: main (lab1.cpp:35)
==22696==
==22696== 52 bytes in 1 blocks are definitely lost in loss record 9 of 12
==22696==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22696==    by 0x40230A: Prog::resize(int) (Prog.cpp:332)
==22696==    by 0x401DD7: Prog::operator=(Prog const&) (Prog.cpp:171)
==22696==    by 0x40201D: Prog::operator*=(Prog const&) (Prog.cpp:214)
==22696==    by 0x40116D: main (lab1.cpp:55)

任何帮助不胜感激!

编辑 - 赋值运算符也显示内存泄漏,但它使用 resize 方法,这就是我省略它的原因,但这里请求的其余代码:

Prog::Prog() : currentSize(1)
{
    pArray = new int[currentSize] {0};
}
Prog::~Prog()
{
    for (int i = 0; i < currentSize; i++)
      this->pArray[i] = 0;
    currentSize = 0;
    delete[] pArray;
    pArray = nullptr;
}

Prog& Prog::operator=(const Prog& rhs)
{
  if (this == &rhs)
    return *this;
  for (int i = 0; i < currentSize; i++)
    pArray[i] = 0;
  if (this->currentSize < rhs.currentSize)
  {
    resize(rhs.currentSize + 1);
    currentSize = rhs.currentSize;
    pArray = new int[currentSize];
    for (int i = 0; i < currentSize; i++)
      pArray[i] = rhs.pArray[i];
  }
  else
  {
    for (int j = 0; j < rhs.currentSize; j++)
      pArray[j] = rhs.pArray[j];
  }
  return *this;
}

在你的operator=中,你调用resize,然后在接下来的两个语句中再次做同样的事情。 由于resize分配内存(并将该指针存储到pArray 中,因此您用新值覆盖该值operator=而不释放以前的值,因此会出现泄漏。