c++ 类分配的动态数组失败(内存泄漏)

标签 c++ class dynamic-memory-allocation

edit1:添加一个正在运行的微型版本。 我写了一个包含一些类的 cpp 文件。当我在单个文件中测试它时,一切正常,但是当我将它与其他 c 文件链接时,我存储在类数组中的数据发生了变化。我知道我的内存分配肯定有问题,所以我使用 new 将其更改为动态内存分配 但不知道在哪里或如何修复

work in single .cpp file
in a file called test.app
class Board
{
public:
    int **grid;
    bool done;
    int result;

public:
    Board()
    {
      grid = new int*[3];
      for(int i = 0; i < 3; ++i){
        grid[i] = new int[3];
      }
      for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                grid[i][j]=0;
            }
        }
        done=false;
        result=0;
    }
}

class Node
{

public:
    Board **arr;
    //double heuristic;
    bool done;
    int result;
    int prevx, prevy;
    int next_turn;

public:

    Node()

    {
       arr = new Board*[3];
       for(int i=0;i<3;i++)
       {
         arr[i] = new Board[3];
       }
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                arr[i][j] = new Board();
            }
        }
        done = false;
        //heuristic=0;
        result = 0;
        prevx = -1;
        prevy = -1;
        next_turn=1;
    }
}

以及出错的代码:

Treenode *head; //treat as global variable

void agent_start( int this_player )
{
  //nothing to do
  //cout << "here all good" << endl;
  head = new Treenode();
  //cout << head << endl;
  m = 0;
  return;

}

int agent_second_move( int board_num, int prev_move )
{
  for(int i=0;i<9;i++)
  {
    for(int j=0;j<9;j++)
    {

      if(head->arr[res_boardx][res_boardy].grid[cordx][cordy] == 1)
      {
        cout << "here cant print" << endl;
        head->move2(i,j,-1);
        cout << "here cant print" << endl;
      }
      else if(head->arr[res_boardx][res_boardy].grid[cordx][cordy] == -1)
      {
        cout << "here cant print" << endl;
        head->move2(i,j,1);
      }
    }
  }
in test.h
extern int   port;
extern char *host;

#ifdef __cplusplus
extern "C" {
#endif
  extern char *host;
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
  //  parse command-line arguments
  void agent_parse_args( int argc, char *argv[] );

  //  called at the beginning of a series of games
  void agent_init();

  //  called at the beginning of each game
  void agent_start( int this_player );

  int  agent_second_move(int board_num, int prev_move );

  int  agent_third_move(int board_num,int first_move,int prev_move);

  int  agent_next_move( int prev_move );

  void agent_last_move( int prev_move );

  //  called at the end of each game
  void agent_gameover( int result, int cause );

  //  called at the end of the series of games
  void agent_cleanup();
  #ifdef __cplusplus
  }
  #endif
in main.cpp
int main(int argc, char *argv[]){
    agent_start(1);
    int b = agent_second_move(1,1);
}

输出是:

[1]    26904 illegal hardware instruction

segmentation fault 

在我宣布之前

class Node
{

public:
    Board arr[3][3]; ///

在 Node 类中。

导致树节点数据发生变化之前的工作版本

class Board
{
public:
    int grid[3][3];
    bool done;
    int result;

public:
    Board()
    {
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                grid[i][j]=0;
            }
        }
        done=false;
        result=0;
    }
}
class Node
{

public:
    Board arr[3][3];
    //double heuristic;
    bool done;
    int grid[3][3];
    int result;
    int prevx, prevy;
    int next_turn;

public:

    Node()
    {
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                arr[i][j] = Board();
                grid[i][j]=0;
            }
        }
        done = false;
        //heuristic=0;
        result = 0;
        prevx = -1;
        prevy = -1;
        next_turn=1;
    }
}

Treenode *head;
head = new Treenode();

void print_map(){
  for(int i=0;i<9;i++)
  {
    for(int j=0;j<9;j++)
    {
      int res_boardx, res_boardy, cordx, cordy;
      res_boardx = (i-1)/3;
      res_boardy = (i-1)%3;
      cordx = (j-1)/3;
      cordy = (j-1)%3;
      cout << head->arr[res_boardx][res_boardy].grid[cordx][cordy];
    }
    cout << endl;
  }
}

当我在此之外调用打印功能时,打印出的二维数组如下 错误的文件,因为它应该是 1 或 0 或 -1。

433000000
107312758200000000
000000000
000000000
000000000
000000000
00000-1000
000000000
000000000

最佳答案

您没有遵循规则 5。您在构造函数中分配了一些内存,因此您需要一个非默认析构函数来正确释放所有内容,只要显式移动/复制构造函数和赋值运算符即可。

如果可能,您应该坚持使用像 std::vector 这样的标准容器,它会为您处理所有极端情况。

关于c++ 类分配的动态数组失败(内存泄漏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810953/

相关文章:

c++ - 为什么通过 goto 后退时调用析构函数

c++ - 扩展 unordered_map,覆盖 operator[],MSVC 2015 错误

python - 使用类来计算平均值Python

php - 处理表连接时如何有效地编写对象类?

c - 仅在不调试时多次调用后 realloc 失败

c++ - ld 没有加载所需的目标文件

c++ - 有没有一种简单的方法可以使用 Visual Studio 2010 的构建系统设置链接接缝?

c++ - 如何在 C++ 中使用类模板启动类构造函数?

c - 重新分配多维数组

c++ - 无法从类内的其他函数访问同一类的变量