-1

This used to happen frequently with me when I use pointers. Today I have one code with similar issue. When I run the below code in debug mode, the code executes perfectly fine and exits normally in Code Blocks 13.12

When I try to run it in normal mode, it seems to be getting a segmentation fault and thus result in code termination. Code basically breaks during the execution of print_board function. But I am not sure why this happens.

Here is my code:

#include <stdio.h>
#include <stdlib.h>

#define boardheight 8
#define boardwidth 8
struct Player_symbols{
    char symbol;
    char king_symbol;
};

struct Checker_piece{
    char king;
    int on_board;
    int num_moves;
    int player;
    struct Player_symbols* symbols;
};

int pieces_count[2] = {12,12};
struct Checker_piece* player_1_pieces;
struct Checker_piece* player_2_pieces;

void initialize_player_pieces(struct Checker_piece* player_pieces, struct Player_symbols* player_symbols, int player_num);
void initialize_board(struct Checker_piece* checker_board[boardheight][boardwidth],struct Checker_piece* player1,struct Checker_piece* player2);
void print_board(struct Checker_piece* checker_board[boardheight][boardwidth]);
int move_piece(struct Checker_piece* checker_board[boardheight][boardwidth], int x,int y,int player);
void continue_jumping(struct Checker_piece* checker_board[boardheight][boardwidth],int* y,int* x,int player);
int generate_destination(int x, int *dest_x, int *dest_y, int *dest_y_jump,int dest_y2,int dest_y2_jump,int move_flags,int player);

int main()
{
    struct Checker_piece*  checker_board[boardheight][boardwidth];
    //declare and initialize the Checker_piece structures by allocating memory dynamically using a dummy structure to determine its size
    player_1_pieces = malloc(12*sizeof (struct Checker_piece));
    player_2_pieces = malloc(12*sizeof (struct Checker_piece));
    struct Player_symbols *player_1_symbols,*player_2_symbols;

    player_1_symbols = malloc(sizeof (struct Player_symbols));
    player_2_symbols = malloc(sizeof (struct Player_symbols));

    //initialize the player symbols
    player_1_symbols->symbol = 'o';
    player_1_symbols->king_symbol = 'O';
    player_2_symbols->symbol = 'x';
    player_2_symbols->king_symbol = 'X';

    initialize_player_pieces(player_1_pieces,player_1_symbols,1);
    initialize_player_pieces(player_2_pieces,player_2_symbols,2);
    initialize_board(checker_board,player_1_pieces,player_2_pieces);
    print_board(checker_board);
    return 0;
}

void initialize_player_pieces(struct Checker_piece* player_pieces, struct Player_symbols* player_symbols, int player_num){
    int i;
    for (i = 0; i < 12; i++, player_pieces++ ) {
        player_pieces->king='N';
        player_pieces->num_moves=0;
        player_pieces->on_board=1;
        player_pieces->player=player_num;
        player_pieces->symbols= player_symbols;
    }
}

void initialize_board(struct Checker_piece* checker_board[boardheight][boardwidth],
                      struct Checker_piece* player1,struct Checker_piece* player2)
{
    int i, j;
    for(i = 0; i < 3; i++) {
        for(j = 0; j <= 7; j = j+2) {
            if(i == 0 || i % 2 == 0) {
                checker_board[i][j+1] = player1;
                checker_board[i][j] = malloc(sizeof (struct Checker_piece));
            } else {
                checker_board[i][j] = player1;
                checker_board[i][j+1] = malloc(sizeof (struct Checker_piece));
            }
            player1++;
        }
    }
    for(; i < 5; i++){
        for(j = 0; j < 8; j++){
            checker_board[i][j] = malloc(sizeof (struct Checker_piece));
        }
    }
    for(i = 5; i < 8; i++){
        for(j = 0; j <= 7; j = j+2){
            if(i == 0 || i % 2 == 0){
                checker_board[i][j+1] = player2;
                checker_board[i][j] = malloc(sizeof (struct Checker_piece));
            } else {
                checker_board[i][j] = player2;
                checker_board[i][j+1] = malloc(sizeof (struct Checker_piece));
            }
            player2++;
        }
    }
}
void print_board(struct Checker_piece* checker_board[boardheight][boardwidth]){
    int i = 0,j = 0;
    printf("\n   1   2   3   4   5   6   7   8\n");
    printf(" |---|---|---|---|---|---|---|---|\n");

    for(i = 0; i < 8; i++) {
        printf("%d|",(i + 1));
        for(j = 0; j < 8; j++) {
                if(checker_board[i][j]->king != '\r')
                    printf(" %c |",checker_board[i][j]->symbols->symbol);
                else
                    printf("   |");
        }
        printf("\n");
        printf(" |---|---|---|---|---|---|---|---|\n");
    }
}
MD XF
  • 7,860
  • 7
  • 40
  • 71
The Cloud Guy
  • 963
  • 1
  • 8
  • 20
  • 1
    What do you think will happen in this line `checker_board[i][j+1] = ...` if `j=6` (and it will be)? – Eugene Sh. Oct 19 '16 at 17:35
  • The problem is somewhere in the print_board function. The rest of the code is working fine. I verified this fact by commenting the print_board function. – The Cloud Guy Oct 19 '16 at 17:49
  • 1
    In this case you might want to understand the meaning of the term *undefined behavior*. – Eugene Sh. Oct 19 '16 at 17:50
  • So far what I understood is that I am facing issue with the check_board pointer for which I allocate memory rather than assigning a piece to it. Like Checker_board[0][0] pointer – The Cloud Guy Oct 19 '16 at 17:54
  • So you don't see a problem with the code I've pointed out?? And I think it's not the only one.. – Eugene Sh. Oct 19 '16 at 17:55
  • why all the dynamic memory allocation? You don't need anything to persist once `main` returns and you know how much memory you want at compile time. – yano Oct 19 '16 at 18:18
  • @AbhishekKothari in the print_board() function, the variable 'checker_board[i][j]->symbols' is not initialized = 0xcdcdcdcd under Visual C++ in debug mode. – J. Piquard Oct 19 '16 at 20:26

1 Answers1

0

When looking the source code and using also the debugger, I have confirm the problem: 1- In the print_board(), the following test-if check a value which is not used '\'r':

if(checker_board[i][j]->king != '\r')

2- In the initialize_board(), all the malloc() are not initialized at least for the 'king' parameter:

void initialize_board(struct Checker_piece* checker_board[boardheight][boardwidth],
                      struct Checker_piece* player1,struct Checker_piece* player2)
{
    int i, j;
    for(i = 0; i < 3; i++) {
        for(j = 0; j <= 7; j = j+2) {
            if(i == 0 || i % 2 == 0) {
                checker_board[i][j+1] = player1;
                checker_board[i][j] = malloc(sizeof (struct Checker_piece));
                // init at least the king to '\r'
                checker_board[i][j]->king = '\r';
            } else {
                checker_board[i][j] = player1;
                checker_board[i][j+1] = malloc(sizeof (struct Checker_piece));
                checker_board[i][j+1]->king = '\r';
            }
            player1++;
        }
    }
    for(; i < 5; i++){
        for(j = 0; j < 8; j++){
            checker_board[i][j] = malloc(sizeof (struct Checker_piece));
            checker_board[i][j]->king = '\r';
        }
    }
    for(i = 5; i < 8; i++){
        for(j = 0; j <= 7; j = j+2){
            if(i == 0 || i % 2 == 0){
                checker_board[i][j+1] = player2;
                checker_board[i][j] = malloc(sizeof (struct Checker_piece));
                checker_board[i][j]->king = '\r';
            } else {
                checker_board[i][j] = player2;
                checker_board[i][j+1] = malloc(sizeof (struct Checker_piece));
                checker_board[i][j+1]->king = '\r';
            }
            player2++;
        }
    }
}
J. Piquard
  • 1,665
  • 2
  • 12
  • 17