Netbeans:Appcrash在运行时(2d动态数组)
Netbeans: Appcrash during runtime(2d dynamic array)
我被要求制作一个2d动态数组,大小等于M*M(用户输入),列和行的大小都是M。
我的问题是输入M和K值后,应用程序崩溃。
我不完全理解构造函数和析构函数在动态数组中的使用,所以我认为我缺少一些东西。如有任何帮助,我们将不胜感激!:)
#include <iostream>
#include <iomanip>
#include <new>
#include <cmath>
using namespace std;
class Board {
private:
bool gameOver; //true if game is finished.
int K; // number of consecutive stones needed to win.
//int board [][];
int M;
int **ptr; // pointer to a pointer(**)
public:
Board();
~Board();
void getBoardSize();
void setArrayIndex();
void printBoard();
int getComputerInput();
int getPlayerInput();
void setIndexValue(int, int);
};
//Constructor to initiate variables.
Board::Board(){
gameOver = false;
M = 0;
K = 0;
//board [*M][*M];
ptr = new int*[M]; // create new array of pointers to int objects.
for (int i=0; i < M; i++){
ptr[i] = new int[M];
}
}
//The destructor to release the heap memory.
Board::~Board(){
for (int i=0;i<M; i++){
delete [] ptr[i];
}
delete[] ptr;
}
void Board::getBoardSize(){
cout << "Enter value for M ( > 2): " << endl;
cin >> M;
cout << "Enter value for K ( > 1): " << endl;
cin >> K;
}
void Board::setArrayIndex(){
for (int i=0; i < M; i++){
for (int j=0; j < M; j++){
ptr [i][j] = 0;
}
}
}
void Board::printBoard(){
for (int i=0; i < M; i++){
cout << setw(4) << i+1; //Print Column numbers first.
}
cout << endl; //column headers done...
for (int r=0; r < M; r++){
cout << setw(2) <<r+1 << " ";
for (int c=0; c < M; c++){
cout << ptr[r][c]; // index values printed.
if (c+1 < M){ //to prevent the last "---".
cout << "---";
}
}
cout << endl;
cout << setw(4);
for (int j=0; j < M; j++){
if (r+1 < M){ //to prevent the last "|".
cout << "|";
}
cout << " ";
}
cout << endl;
}
}
int Board::getComputerInput(){
int x, y;
cout << "PC: Input row and column (x, y) from 1 to M: " << endl;
cin >> x;
cin >> y;
cout << "PC Plays " << "(" << x << ", " << y << ")" << endl;
printBoard();
return x-1, y-1;
}
int Board::getPlayerInput(){
int x, y;
cout << "Human: Input row and column (x, y) from 1 to M: " << endl;
cin >> x;
cin >> y;
cout << "Human Plays " << "(" << x << ", " << y << ")" << endl;
printBoard();
return x-1, y-1;
}
void Board::setIndexValue(int a, int b){
//ptr [a][b] =
}
#endif /* NEWFILE_H */
Main
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
Board object;
object.getBoardSize();
object.setArrayIndex();
object.printBoard();
//object.getComputerInput();
//object.getPlayerInput();
return 0;
}
首先检查您是否为M
输入了太大的值,因为如果没有足够的可用空间,这肯定会使程序崩溃。
其次,使用gdb这样的调试器来运行程序。它必须在Netbeans中也可用。当应用程序崩溃时,它将指出崩溃的位置和故障类型。
您可以查看调用堆栈和变量的值来查找崩溃的原因。
相关文章:
- 基于用户输入的 2D 动态阵列
- 2D 动态阵列被视为特洛伊木马?
- 打印 2D 动态数组 c++ 函数
- 不明白使用双指针 (**) 创建 2d 动态数组
- 英特尔并行工作室 2015 C++中的 2D 动态分配全局阵列
- 如何在C 中的2D动态数组中访问1个元素
- C 类中的2D动态数组
- C 删除并在2D动态阵列中删除和紧凑的行
- C++ 2D 动态阵列分配
- 显示2D动态数组
- 为具有许多友元函数c++的模板类(2d动态)创建析构函数
- 如何防止此 2D 动态数组程序崩溃
- 访问 2D 动态数组的元素 - c++
- 在 2D 动态数组中填充数据
- 填充 2D 动态数组时"无法读取内存"
- 对 2D 动态数组进行排序 C++
- 2D 动态数组程序崩溃
- c++11 使用 std::fill 来填充 2D 动态分配的数组
- 类和 2D 动态数组
- 将一个2d动态数组传递给C++中的一个函数