0

I have been asked to make a 2d dynamic array, with a size equal to M*M (user input), column and row are both the same size M.

My problem is after entering M and K values the app crashes.

I don't fully understand the constructor and destructor use in dynamic arrays so I think there is something I'm missing there. Any help would be very appreciated! :)

#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;
}
Shayan
  • 652
  • 5
  • 12
  • The basic answer is that you need to initialize `ptr` in a loop like it is done in this question: http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new, but a better answer is to turn `ptr` into an [`std::array>`](http://en.cppreference.com/w/cpp/container/array) so you don't have to deal with memory allocation (if you can use C++11). – IllusiveBrian Sep 07 '14 at 04:47
  • Hi, I have done similar initialisation of array using for loop inside the constructor but the program still crashes. I dont have support for C++11 unfortunately. – Shayan Sep 07 '14 at 05:55

1 Answers1

0

Firstly check that you are not entering too big value for M, as that will surely crash the program if there isn't sufficient space available.

Secondly use a debugger like gdb to run your program. It must be available in Netbeans also. When the application crashes, it will pin point the location where it crashed and the type of fault.

You can look at call-stack, and value of variables to find the reason of crash.

Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80
  • Hi, thanks for your input. The value of M entered is always below 100. That shouldn't be a problem. I did try the debugger, it says "Signal received: SIGSEGV (Segmentation Fault)." I'm not quite sure why that is. – Shayan Sep 07 '14 at 08:38
  • The most common reason for SIGSEGV is out-of-bounds access, accessing null pointer etc. Look carefully at stack-trace and try to figure out location of illegal memory operation. – Vinayak Garg Sep 07 '14 at 08:52
  • Thanks guys. I figured it out using the debugger it told me the problem was in function SetArrayIndex() creating 0 size array because in constructor I had M=0 then the for loop initialisation right after that. I moved the for loop outside and its fixed! – Shayan Sep 07 '14 at 10:10