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

问题描述 投票:1回答:1

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

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类中。

之前工作版本导致treenode中的数据发生了变化

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;
  }
}

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

433000000
107312758200000000
000000000
000000000
000000000
000000000
00000-1000
000000000
000000000
c++ class dynamic-memory-allocation
1个回答
1
投票

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

如果可能的话,你应该坚持使用像std::vector这样的标准容器来处理所有角落的情况。

© www.soinside.com 2019 - 2024. All rights reserved.