Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do I get rid of this error in my code: "segmentation error (core dumped)"?

+1 vote
asked Mar 11, 2021 by Michael Fabian (190 points)
Here is my code. I assume this is a small error that I have made in one of my functions. It started happening after I implemented the "get_int" function and the "check_row" function. Any help or other suggestions would be greatly appreciated.

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>

void get_int(int&, int&);
void check_row(char**, int&, int&, int&);
void print_board(char **, int, int);
void initialize_board(char **, int, int);
void update_board(char**, int, int, int&);
void delete_board(char**, int);
void check_args(int, char**, int&, int&, int&);
void reprompt_args(int&, int&, int&);
void error();

using namespace std;

int main(int argc, char** argv) {
    int cols, rows, players, player = 1, col;
    
    check_args(argc, argv, cols, rows, players);

    char** board = new char*[rows];
    for (int i = 0; i < rows; i++)
        board[i] = new char[cols];

    initialize_board(board, cols, rows);
    get_int(col, cols);
    check_row(board, col, rows, player);
    print_board(board, cols, rows);
    //update_board(board, cols, rows, col);
    delete_board(board, rows);

    return 0;
}

void get_int(int& col, int& cols) {
    while((!(cin >> col)) || (col > cols)) {
        error();
    }
}

void check_row(char** board, int& col, int& rows, int& player) {
    int a = 0;
    if ((board[col-1][a]!=0) && (a < rows)) {
        a++;
        check_row(board, col, rows, player);
    }
    else if(player == 1 && a < rows) {
        board[col-1][a] == 'X';
        a=0;
    }
    else if (player == 2 && a < rows) {
        board[col-1][a] == 'O';
        a=0;
    }
    else {
        cout << "Invalid Row Try Again!" << endl;
        a=0;
        player++;
    }
}

void print_board(char ** board, int cols, int rows) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (i % 2 == 0 && j % 2 == 0)
                cout << "|\033[30;47m " << board[i][j] << " ";
            else if (i % 2 == 1 && j % 2 == 1)
                cout << "|\033[30;47m " << board[i][j] << " ";
            else
                cout << "|\033[0m " << board[i][j] << " ";
            cout << "\033[0m";
        }   
        cout << endl;
    }

}

void delete_board(char** board, int rows) {
    for(int i = 0; i < rows; i++)
        delete[] board[i];
    delete[] board;  
}

void update_board(char** board, int cols, int rows, int& col) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            board[i][j] = 'X';
    }
    print_board(board, cols, rows);
}

void initialize_board(char ** board, int cols, int rows) {
    for (int i = 0; i < cols; i++)
        cout << " " << (i + 1) << " ";
    cout << endl;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (i % 2 == 0 && j % 2 == 0)
                cout << "|\033[30;47m " << board[i][j] << " ";
            else if (i % 2 == 1 && j % 2 == 1)
                cout << "|\033[30;47m " << board[i][j] << " ";
            else
                cout << "|\033[0m " << board[i][j] << " ";
            cout << "\033[0m";
        }   
        cout << endl;
    }
    cout << endl;
}

void check_args(int argc, char** argv, int& cols, int& rows, int& players) {
    int arg;
    
    for(int i = 1; i < argc; i++) {
        if (atoi(argv[i]) == 0) {
            cout << "Invalid Argument #" << i << "!" << endl;
            arg = 0;
        }       
    }
    if ((atoi(argv[1]) > 2) || (atoi(argv[2]) > 20) || (atoi(argv[3]) > 20) || (argc != 3))
        arg = 0;
    
    if (arg == 1) {
        players = atoi(argv[1]);
        rows = atoi(argv[2]);
        cols = atoi(argv[3]);
    }
    else {
        reprompt_args(players, rows, cols);
    }
}

void reprompt_args(int& players, int& rows, int& cols) {
    cout << "Invalid Command Line Arguments!" << endl << endl;
    cout << "Enter Players: ";
    while((!(cin >> players)) || (players > 2) || (players < 1)) {
        error();
    }
    cout << "Enter rows: ";
    while((!(cin >> rows)) || (rows > 20) || (rows < 2)) {
        error();
    }
    cout << "Enter cols: ";
    while((!(cin >> cols)) || (cols > 20) || (cols < 2)) {
        error();
    }
}

void error() {
    cout << "Error! Enter a valid input: " << endl;
    cin.clear();
    cin.ignore(1024, '\n');
}

2 Answers

0 votes
answered Mar 13, 2021 by keshavi arora (140 points)

Hi

following is causing segmentation fault. do commnet hat and try.

if (/*(atoi(argv[1]) > 2) ||*/(atoi(argv[2]) > 20) || (atoi(argv[3]) > 20) || (argc != 3)) 
        arg = 0;
    

Thanks

+1 vote
answered Mar 13, 2021 by xDELLx (10,380 points)

couldnt find a seg fault when run with proper arguments.

I would suggest check how to use debugger (like gdb). You can analyse the core dumpsand figure where exactly the issue occurs.

I would suggest change line

  if ((atoi(argv[1]) > 2) || (atoi(argv[2]) > 20) || (atoi(argv[3]) > 20) || (argc != 3))
        arg = 0;

to

if ((argc < 3) || (atoi(argv[1]) > 2) || (atoi(argv[2]) > 20) || (atoi(argv[3]) > 20)  )
        arg = 0;

Also make sure you provide input you tried where the error triggers.

Document your code,it will help others understand the logic(i didnt !! :) )

Initialise all variables before use.

Reprompt code where 'players' are validated ,seems funny to me.

commented Mar 13, 2021 by KKN (1,110 points)
My guy's on a roll!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...