C ++有类析构函数解除分配2d数组的问题

时间:2012-10-02 01:21:48

标签: c++

第一次发布海报,yadayada。从事家庭作业,我遇到了让析构函数正常工作的问题。程序将成功编译,但是当它运行时,它会抛出:

*** glibc detected *** ./bmain: munmap_chunk(): invalid pointer: (random memory address)

我已经确定这是由我的析构函数引起的,因为当我发表评论时,一切正常。我玩过析构函数,它的当前形式是我认为应该是正确的,但显然不是。有人能向我指出我做错了什么吗?如果我发布过多代码,我会提前道歉,但我宁愿提供信息过载,而不是不够。

gameBoard.h

#include <iostream>
#include <iomanip>
#include <string>

const int ROW_MIN = 3;
const int ROW_MAX = 50;
const int COL_MIN = 3;
const int COL_MAX = 50;

using namespace std;

class gameBoard
{
    public:

       gameBoard(int x = ROW_MIN, int y = COL_MIN);

       ~gameBoard();

       void setCell(int x = 0, int y = 0, char piece = '?');

       char getCell(int x = 0, int y = 0) const;

       void printBoard();

   protected:

      int rows;
      int cols;
      char **board;
};

gameBoardImp.cpp

#include "gameBoard.h"

gameBoard::gameBoard(int x, int y)
{
    if (x < ROW_MIN || x > ROW_MAX)
    {
       cout << "Error.  Invalid game board size.  Number of rows must be between " << ROW_MIN << " and " << ROW_MAX << "." << endl;
       cout << "Board not created." << endl;
    }
    else if (y < COL_MIN || y > COL_MAX)
    {
       cout << "Error.  Invalid game board size.  Number of columns must be between " << COL_MIN << " and " << COL_MAX << "." << endl;
       cout << "Board not created." << endl;
    }
    else
    {
       rows = x;
       cols = y;
       board = new char* [rows];
          for (int row = 0; row < rows; row++)
          {
             board[row] = new char[cols];
             for (int col = 0; col < cols; col++)
             {
                board[row][col] = '?';
             }
          }
    }
}

gameBoard::~gameBoard()
{
   if (board != NULL)
   {
      for (int i = 0; i < rows; i++)
      {
         delete [] board[i];
      }
      delete []board;
      board = NULL;
   }
}

void gameBoard::setCell(int x, int y, char piece)
{
   if (x > rows || x < 0)
   {
      cout << "Error.  Invalid board location.  X has to be between 0 and " << (rows - 1) << " ." << endl;
   }

   else if (y > cols || y < 0)
   {
      cout << "Error.  Invalid board location.  Y has to be between 0 and " << (cols - 1) << " ." << endl;
   }

   else board[x][y] = piece;
}

char gameBoard::getCell(int x, int y) const
{
   if (x > rows || x < 0)
   {
      cout << "Error.  Invalid board location.  X has to be between 0 and " << (rows - 1) << " ." << endl;
   }

   if (y > cols || y < 0)
   {
      cout << "Error.  Invalid board location.  Y has to be between 0 and " << (cols - 1) << " ." << endl;
   }

   return board[x][y];
}

void gameBoard::printBoard()
{
   cout << " " << setw(cols) << setfill('-') << "-" << " " << endl;
   for (int i = 0; i < rows; i++)
   {
      cout << "|";
      for (int j = 0; j < cols; j++)
      {
         cout << board[i][j];
      }
      cout << "|" << endl;
   }
   cout << " " << setw(cols) << setfill('-') << "-" << " " << endl;
}

bmain.cpp

// CS 202 Provided Main


#include <iostream>
#include <iomanip>
#include <string>

#include "gameBoard.h"

using namespace std;

int main()
{
// ---------------------------------------
//  Some valid declarations

    gameBoard brd1(10, 10);
    gameBoard brd2(8, 8);

// ---------------------------------------
//  Some invalid declarations

    cout << endl << "*** Invalid declarations -> should show errors." << endl;
    cout << endl;
    gameBoard brd3(1, 1);           // error
    gameBoard brd4(9, 60);          // error

// ---------------------------------------
//  Try board #1

    cout << endl;
    cout << "*** Board #1 *******************************" << endl;
    cout << endl;

    {
        brd1.printBoard();

        for (int i=0; i<10; i++)
            brd1.setCell(i,i,'x');

        cout << endl;
        cout << "Cell (0,1) is :" << brd1.getCell(0,1) << endl;
        cout << "Cell (1,1) is :" << brd1.getCell(1,1) << endl;
        cout << "Cell (1,0) is :" << brd1.getCell(1,0) << endl << endl;

        brd1.printBoard();
    }
// Note, brd1 goes out of scope here...

//  uncommenting this print board will crash if the destructor works correctly.
//  brd1.~gameBoard();

// ---------------------------------------
//  Try board #2

    cout << endl;
    cout << "*** Board #2 -> Error Testing *************" << endl;
    cout << endl;
    brd2.setCell(10,10,'x');        // error
    brd2.setCell(1,10,'x');         // error
    brd2.setCell(10,1,'x');         // error
    brd2.setCell(5,-1,'x');         // error
    brd2.setCell(-5,1,'x');         // error
    cout << endl;

    cout << endl;
    cout << "*** Board #2 *******************************" << endl;
    cout << endl;


    for (int i=0; i<8; i++)
        brd2.setCell(i,i,'x');

    for (int i=0, j=7; i<8; i++, j--)
        brd2.setCell(i,j,'y');

    brd2.printBoard();

    cout << endl;
    cout << "********************************************" << endl;
    cout << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:4)

xy无效的构造函数中,您没有将board设置为NULL,因此它未被初始化。这会导致析构函数delete[]无效指针。