初ポスター、やだやだ。宿題に取り組んでいますが、デストラクタを適切に動作させるのに問題があります。プログラムは正常にコンパイルされますが、実行すると次のようにスローされます。
*** 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;
}