C++ C++;类析构函数取消分配2d数组时出现问题
第一次海报,yadayada。在做家庭作业时,我遇到了让析构函数正常工作的问题。程序将成功编译,但运行时会抛出:C++ C++;类析构函数取消分配2d数组时出现问题,c++,C++,第一次海报,yadayada。在做家庭作业时,我遇到了让析构函数正常工作的问题。程序将成功编译,但运行时会抛出: *** glibc detected *** ./bmain: munmap_chunk(): invalid pointer: (random memory address) 我已经确定这是由我的析构函数引起的,因为当我把它注释出来时,一切都正常。我玩过析构函数,它的当前形式是我认为应该正确的,但显然不是。有人能指出我做错了什么吗?如果我发布了太多的代码,我会提前道歉,但我宁愿提
*** 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;
};
#包括
#包括
#包括
常量int行_MIN=3;
第行常量_MAX=50;
const int COL_MIN=3;
const int COL_MAX=50;
使用名称空间std;
类游戏板
{
公众:
游戏板(INTX=行最小值,INTY=列最小值);
~gameBoard();
void setCell(intx=0,inty=0,字符片段='?');
chargetcell(intx=0,inty=0)常数;
作废印刷板();
受保护的:
int行;
int cols;
炭板;
};
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;
}
#包括“gameBoard.h”
游戏板::游戏板(整数x,整数y)
{
如果(x<行最小值| | x>行最大值)
{
当x
或y
无效时,构造函数中的cout未将board
设置为NULL,因此未对其进行初始化。这会导致析构函数delete[]
无效指针。在构造函数中,当x
或y
无效时,您没有将board
设置为NULL,因此它未初始化。这会导致析构函数delete[]
无效指针。如果使用合适的容器,则不需要析构函数。虽然不相关:在边界检查中,x>行
应该是x>=行
,而y
同样。@chris合适的容器?你能详细说明一下吗?我是个新手(这是我大学轮换的第二个CS类)我喜欢额外的学习。科学家切特:既然我在看一个数组,它不应该是x>行吗?例如,如果行=3,那么数组[rows]给出数组[0]、数组[1]和数组[2]。因此,如果x是3,它就在数组的边界之外,对吗?@RonHolcomb,我喜欢它的解释方式,但是如果你是new[]
创建一个数组,可以使用std::vector
,或者更合适的方法,但向量是通用的。如果你是新的
对象,请使用智能指针。这些管理类都不需要与指针相同的任何额外逻辑来确保它们正确复制/移动/删除。如果使用合适的容器。虽然不相关:在你的边界检查中,x>行
应该是x>=行
,而y
同样如此。@chris合适的容器?你能详细说明一下吗?我是个新手(这是我大学轮换的第二个CS类)我喜欢额外的学习。科学家切特:既然我在看一个数组,它不应该是x>行吗?例如,如果行=3,那么数组[rows]给出数组[0]、数组[1]和数组[2]。因此,如果x是3,它就在数组的边界之外,对吗?@RonHolcomb,我喜欢它的解释方式,但是如果你是new[]
创建一个数组,可以使用std::vector
,或者更合适的方法,但向量是通用的。如果你是新的
,请使用智能指针。这些管理类都不需要与指针相同的任何额外逻辑来确保它们正确复制/移动/删除。约翰,非常感谢你的正确答案。我很抱歉约翰,非常感谢你给出的正确答案。我已经为此挣扎了大约半个小时,我正在为这么一件小事而自责。
// 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;
}