c++ - Valgrind 无效读取大小 4 错误?

标签 c++ memory memory-management

尝试释放列表后,我在 Valgrind 中收到此错误。

这是Valgrind的重要部分

    ==12349== Invalid read of size 4
    ==12349==    at 0x8048BB4: DynArray::addMovie(Movie*) (in /home/admin/a2/mbd)
    ==12349==    by 0x8048D11: main (in /home/admin/a2/mbd)
    ==12349==  Address 0x4321060 is 0 bytes after a block of size 0 alloc'd
    ==12349==    at 0x402B454: operator new[](unsigned int) (in         /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==12349==    by 0x8048B62: DynArray::DynArray() (in /home/admin/a2/mbd)
    ==12349==    by 0x8048C87: main (in /home/admin/a2/mbd)
    ==12349== 
    ==12349== Invalid read of size 4
    ==12349==    at 0x8048BB4: DynArray::addMovie(Movie*) (in /home/admin/a2/mbd)
    ==12349==    by 0x8048D97: main (in /home/admin/a2/mbd)
    ==12349==  Address 0x4321114 is 0 bytes after a block of size 4 alloc'd
    ==12349==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==12349==    by 0x8048B90: DynArray::addMovie(Movie*) (in /home/admin/a2/mbd)
    ==12349==    by 0x8048D11: main (in /home/admin/a2/mbd)
    ==12349== 
    ==12349== Invalid read of size 4
    ==12349==    at 0x8048BB4: DynArray::addMovie(Movie*) (in /home/admin/a2/mbd)
    ==12349==    by 0x8048E1D: main (in /home/admin/a2/mbd)
    ==12349==  Address 0x43211d0 is 0 bytes after a block of size 8 alloc'd
    ==12349==    at 0x402B454: operator new[](unsigned int) (in                         /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==12349==    by 0x8048B90: DynArray::addMovie(Movie*) (in /home/admin/a2/mbd)
    ==12349==    by 0x8048D97: main (in /home/admin/a2/mbd)

DynArray.cc 的重要部分

    DynArray::DynArray() {
      size = 0;
      array = new Movie *[size];
    }        

    void DynArray::addMovie(Movie* m) {
      size++;
      Movie **temp = new Movie *[size];
      for (int x = 0; x < size; x++) {
        temp[x] = array[x];
      }
      delete [] array;
      temp[size-1] = m;
      array = temp;
    }

    void DynArray::cleanup() {
      for (int x = 0; x < size; x++)
        delete array[x];
      delete [] array;
    }

    int main() {
      DynArray *a = new DynArray();
      Movie *m = new Movie("sa", 2129, C_COMEDY);
      a->addMovie(m);
      Movie *k = new Movie("sas", 4324, C_DRAMA);
      a->addMovie(k);
      Movie *l = new Movie("dsad", 43241, C_DRAMA);
      a->addMovie(l);
      for (int x = 0; x < a->size; x++) {
        a->array[x]->printMovie();
      }
      a->cleanup();
      delete a;
      return 0;
    }

电影类并不是那么特别,如果需要我可以发布它

最佳答案

函数addMovie无效。

void DynArray::addMovie(Movie* m) {
  size++;
  Movie **temp = new Movie *[size];
  for (int x = 0; x < size; x++) {
    temp[x] = array[x];
  }
  delete [] array;
  temp[size-1] = m;
  array = temp;
}

您增加了对象大小并尝试访问索引大小为 - 1 的数组元素,但该元素在数组中不存在。循环必须是这样的

  for (int x = 0; x < size - 1; x++) {
    temp[x] = array[x];
  }

关于c++ - Valgrind 无效读取大小 4 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21700761/

相关文章:

c - 为什么我的变量在我的 C 程序中递归调用时改变值?

c++ - 如何在 Sieve_of_Eratosthenes 中使用更少的内存

c# - 为什么程序集的调用会占用如此多的 RAM?

c++ - 在函数中分配内存并返回它

c++ - 无法在 igraph 上生成 igraph_degree_sequence_game

c++ - 为什么遗留 C 标识符不需要 namespace 标准?

c - C 结构的数据对齐

iphone - 保留使用 UIGraphicsGetImageFromCurrentImageContext 创建的 UIImage 的所有权

c++ - 共享指针 : why no double free?

c# - 使用命名管道的两种 C++ 到 C# 通信