c++ - 动态数组 C++ 的 Valgrind 内存泄漏

标签 c++ arrays dynamic memory-leaks valgrind

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

.h文件

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

.cpp

void Prog::resize(int newSize)
{
  cout << "Address of polynomial:\t\t\t" << &pArray << endl;
  int* temp = new int[newSize] {0};
  cout << "Address of temp:\t\t\t" << &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;
} 

这是我从 Valgrind 运行我的程序时的输出。

程序截图

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

Valgrind

==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)

感谢任何帮助!

编辑 - 赋值运算符也显示内存泄漏,但它使用调整大小方法,这就是我将其排除在外的原因,但此处请求的其余代码:

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= 中的值,而无需释放先前的值,你得到了泄漏。

关于c++ - 动态数组 C++ 的 Valgrind 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48255298/

相关文章:

c++ - C++中的模板调试

c++ - 词频 strcmp 使用结构数组无限工作

javascript - 如何动态加载 google client.js

c# 鼠标动态面板

php - 如何在动态网页中使用名称而不是 id

c++ - 检查类型是否来自特定命名空间

c++ - 如何将 QVector 与多个对象一起使用

c++ - 将数组本地抛出到 try block

arrays - 为什么选择 -index 数组不起作用,powershell

c++ - GLM 从 vec3 生成旋转矩阵