segmentation fault when creating connect 4 board c program

旧时模样 提交于 2019-12-12 02:27:21

问题


I'm creating a connect-4 game... I have a lot of it done; however, the way I was creating my board was static & it needed to be dynamic, so I've made a side program to fix this before implementing it in my main program. For some reason, the if & else-if conditionals in this chunk of code create a segmentation fault, and I can't figure out why...

  // for the rows/columns of the board
  for(row = num_rows - 1; row >= 0; row--){
      printf("|");
      for(col = 0; col < num_columns; col++){
         if(aPtr[row][col] == '0') {
            printf("| X ");
         }
         else if(aPtr[row][col] == '1') {
            printf("| O ");
         }
         else {
              printf("|   ");
         }      
      }
      puts("||");
   }

when I comment these conditionals out the board prints just fine & looks like this

------ Connect *Four ------
Connect X Command Line Game
&&===================&&
||   |   |   |   |   ||
||   |   |   |   |   ||
||   |   |   |   |   ||
||   |   |   |   |   ||
||   |   |   |   |   ||
||   |   |   |   |   ||
||   |   |   |   |   ||
||   |   |   |   |   ||
||   |   |   |   |   ||
||   |   |   |   |   ||
&&===================&&
   1   2   3   4   5  

the entirety of this side-program is below, any insight as to why this segmentation fault is occurring will be appreciated.

    #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>
            #include <sys/stat.h>


            void initialize(int num_rows, int num_cols, char **aPtr) {
              int i, r, c;

                    // create the space for the board
                    aPtr = malloc(num_rows * sizeof(char*));

                    for (i = 0; i < num_rows; i++){
                        aPtr[i] = malloc(num_cols * sizeof (char));
                    }

                    // go through the board and set all values equal to -1
                    for (r = 0; r < num_rows; r++) {
                        for (c = 0; c < num_cols; c++) {
                            aPtr[r][c] = '9';
                            printf("%c", aPtr[r][c]);
                        }
                        printf("\n");
                    }
                }


            void printBoard(int num_rows, int num_columns, char **aPtr) {
               int row, col; 

               printf("\n");
               puts("------ Connect *Four ------");
               puts("Connect X Command Line Game");

               // for fancy top of board frame
               printf("&&");
               for(col = 1; col < num_columns; col++) {
                printf("====");
               }
               printf("===");
                printf("&&\n");

                // for the rows/columns of the board
               for(row = num_rows - 1; row >= 0; row--){
                  printf("|");
                  for(col = 0; col < num_columns; col++){
                    //  if(aPtr[row][col] == '0') {
                    //      printf("| X ");
                    //  }
                   //  else if(aPtr[row][col] == '1') {
                   //    printf("| O ");
                   //  }
                    // else {
                        printf("|   ");
                    // }      
                  }
                  puts("||");
               }

               // for fancy bottom of board frame
               printf("&&");
               for(col = 1; col < num_columns; col++) {
                printf("====");
               }
                printf("===");
                printf("&&\n");
                printf("  ");
                if (col < 100){
                  for(col = 0; col < num_columns; col++) {
                    if (col < 10) {
                      printf(" %d  ", col + 1);
                    }
                    else {
                      printf("%d  ", col + 1);
                    }
                 }
                 puts("\n");
                }
            }

            // *******************************************************************************************************
            // *******************************************************************************************************

            int main (int argc, char *argv[]) {

                char **aPtr;
                int height = 10;
                int width = 5;
                int i;


                initialize(height, width, aPtr);
                printBoard(height, width, aPtr);
            }

回答1:


Here is the modification of your code, maybe it will help. Note that I'm passing &aPtr and *aPtr = (char*) malloc(...)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>


void initialize(int num_rows, int num_cols, char **aPtr) {
    int i, r, c;

    // create the space for the board
    *aPtr = (char*) malloc(num_rows * sizeof(char*));

    if(*aPtr == NULL)
    {
        free(*aPtr);
        printf("Memory allocation failed");
    }

    for (i = 0; i < num_rows; i++){
        aPtr[i] = (char *) malloc(num_cols * sizeof (char));
    }

    // go through the board and set all values equal to -1
    for (r = 0; r < num_rows; r++) {
        for (c = 0; c < num_cols; c++) {
            aPtr[r][c] = '9';
            printf("%c", aPtr[r][c]);
        }
        printf("\n");
    }
}


void printBoard(int num_rows, int num_columns, char **aPtr) {
    int row, col;

    printf("\n");
    puts("------ Connect *Four ------");
    puts("Connect X Command Line Game");

    // for fancy top of board frame
    printf("&&");
    for(col = 1; col < num_columns; col++) {
        printf("====");
    }
    printf("===");
    printf("&&\n");

    // for the rows/columns of the board
    for(row = num_rows - 1; row >= 0; row--){
        printf("|");
        for(col = 0; col < num_columns; col++){
              if(aPtr[row][col] == '0') {
                  printf("| X ");
              }
              else if(aPtr[row][col] == '1') {
                printf("| O ");
              }
            else {
                printf("|   ");
            }
        }
        puts("||");
    }

    // for fancy bottom of board frame
    printf("&&");
    for(col = 1; col < num_columns; col++) {
        printf("====");
    }
    printf("===");
    printf("&&\n");
    printf("  ");
    if (col < 100){
        for(col = 0; col < num_columns; col++) {
            if (col < 10) {
                printf(" %d  ", col + 1);
            }
            else {
                printf("%d  ", col + 1);
            }
        }
        puts("\n");
    }
}

// *******************************************************************************************************
// *******************************************************************************************************

int main (int argc, char *argv[]) {

    char *aPtr;
    int height = 10;
    int width = 5;
    int i;


    initialize(height, width, &aPtr);
    printBoard(height, width, &aPtr);
}

Note that you are doing: aPtr[r][c] = '9'; so your board is empty, but if you change it to say 0you would get something like:

------ Connect *Four ------
Connect X Command Line Game
&&===================&&
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
&&===================&&
   1   2   3   4   5  

I'm assuming that's what you expected?



来源:https://stackoverflow.com/questions/42590796/segmentation-fault-when-creating-connect-4-board-c-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!