遇到可识别的分段错误

时间:2018-03-09 02:36:59

标签: c segmentation-fault runtime-error

每当我运行代码时,我都会收到一条消息:Segmentation fault: 11

我的问题是为什么会弹出这条消息。我之前做了一些研究,并尝试修复它,但我仍然弹出相同的消息。请查看我的代码!

为我的代码提供一些上下文:该程序的目标是在计算机和人类玩家之间实现Connect Four游戏。函数将在connect4_functions.c文件中实现。这些函数的前向声明在connect4_functions.h文件中。 connect4.c包含main()函数。

代码(connect4_functions.c)如下:

#include "connect4_functions.h"

int print_welcome(void) // PROBLEM: returnerer alltid 1
{
    printf ("*** Welcome to the Connect Four game!!! ***\n");
    printf ("Would you like to make the first move [y/n]: ");

    if (getchar() == 'N' || getchar() == 'n') return 2;
    else return 1;

    while (getchar()!= '\n');
}

void display_board(int board[][BOARD_SIZE_VERT])
{
    int i, j;
    for (i = BOARD_SIZE_VERT-1; i >= 0; i--)
    {
            for (j = 0; j < BOARD_SIZE_HORIZ; j++) printf ("+---");
            printf ("+\n");
            for (j = 0; j < BOARD_SIZE_HORIZ; j++)
            {
                    switch (board[j][i])
                    {
                            case 0: printf ("|   "); break;
                            case 1: printf ("| O "); break;
                            case 2: printf ("| X "); break;
                    }
            }
    printf ("|\n");

    } // end for

    for (j = 0; j < BOARD_SIZE_HORIZ; j++) printf ("+---");
    printf ("+\n");

    for (j = 1; j <= BOARD_SIZE_HORIZ; j++) printf ("  %d ", j);
    printf ("\n");


} //end function display_board



int random_move(int board[][BOARD_SIZE_VERT], int computer_num)
{
    int m = (rand() % BOARD_SIZE_HORIZ) + 1;
    if (!is_column_full(board,m))
    {
            update_board(board,m,computer_num);
            return m;
    }
    else return random_move(board,computer_num);
}

int player_move(int board[][BOARD_SIZE_VERT], int player_num)
{
int m;

    printf ("Please enter your move: ");
    scanf ("%d", &m);

    while (getchar() != '\n');

    if ( 0 > m || m > BOARD_SIZE_HORIZ)
    {
            printf ("Not a valid move. Enter a column number!\n");
            return player_move(board, player_num);
    }

    if (is_column_full(board, m))
    {
            printf ("This column is full. Try again!\n");
            return player_move(board, player_num);
    }

    update_board(board,m,player_num);
    return m;
}


bool check_win_or_tie(int board[][BOARD_SIZE_VERT], int last_move)
{
    int m, count = 0;

    if (check_winner(board, last_move))
    {
            printf("Player %c won!\n", ( check_winner(board,last_move) == 1 ? '1' : '2' ) );
            return true;
    }

    for (m = 0; m < BOARD_SIZE_HORIZ; m++) if ( is_column_full(board, m) ) count++;

    if (count == BOARD_SIZE_HORIZ)
    {
            printf ("Tie game!\n");
            return true;
    }
    else return false;
}

bool is_column_full(int board[][BOARD_SIZE_VERT], int m)
{
    return (board[m-1][BOARD_SIZE_VERT-1]);
}

void update_board(int board[][BOARD_SIZE_VERT], int m, int player_num)
{
    int i;
    for ( i = 0; i < BOARD_SIZE_VERT ; i++)
    {
            if (!board[m-1][i])
            {
                    board[m-1][i] = player_num;
                    return;
            }
    }
}

int check_winner(int board[][BOARD_SIZE_VERT], int last_move)
{
    int i, row, count;

    // Find row
    for (row = 0; row < BOARD_SIZE_VERT; row++)
    {
            if (board[last_move-1][row]) count++;
    }
    row = count;
    printf ("row = %d\n", row);

    // Vertical
    for (i = 0; i < BOARD_SIZE_VERT; i++)
    {
            if (board[last_move-1][i] == board[last_move-1][row]) count++;
            else count = 0;

            if (count == 4) return board[last_move-1][row];
            else return 0;
    }


    count = 0; // reset

    // Horizontal
    for (i = 0; i < BOARD_SIZE_HORIZ; i++)
    {
            if (board[i][row] == board[last_move-1][row]) count++;
            else count = 0;

            if (count == 4) return board[last_move-1][row];
            else return 0;
    }


    count = 0; // reset
return 0;
}

connect4_functions.h的代码(无法更改)如下:

#ifndef CONNECT4_FUNCTIONS
#define CONNECT4_FUNCTIONS


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


#define BOARD_SIZE_HORIZ 7
#define BOARD_SIZE_VERT 6


int print_welcome(void);


void display_board(int board[][BOARD_SIZE_VERT]);


int random_move(int board[][BOARD_SIZE_VERT], int computer_num);


int player_move(int board[][BOARD_SIZE_VERT], int player_num);


bool check_win_or_tie(int board[][BOARD_SIZE_VERT], int last_move);


bool is_column_full(int board[][BOARD_SIZE_VERT], int m);


void update_board(int board[][BOARD_SIZE_VERT], int m, int player_num);


int check_winner(int board[][BOARD_SIZE_VERT], int last_move);


int best_move(int board[][BOARD_SIZE_VERT], int computer_num);


#endif

connect4.c的代码(也无法更改)如下:

#include "connect4_functions.h"

int main()
{
   int board[BOARD_SIZE_HORIZ][BOARD_SIZE_VERT] = { {0} };
   int player_num, computer_num;
   int last_move;


   /* Ask Alice if she wants to go first */
   player_num = print_welcome();
   if (player_num == 1) computer_num = 2;
   else computer_num = 1;

   /* If Alice wants to go first, let her make a move */
   if (player_num == 1)
   {
      display_board(board);
      last_move = player_move(board,player_num);
      display_board(board);
   }


   /* The main loop */

   while (1)
   {
      /* Make a computer move, then display the board */
      last_move = random_move(board,computer_num);
      printf("Computer moved in column: %d\n", last_move);
      display_board(board);

      /* Check whether the computer has won */
      if (check_win_or_tie(board,last_move)) return 0;


      /* Let Alice make a move, then display the board */
      last_move = player_move(board,player_num);
      display_board(board);

      /* Check whether Alice has won */
      if (check_win_or_tie(board,last_move)) return 0;


   } /* end of while (1) */

} /* end of main() */

另外,如果您想查看原始pdf,请链接如下:

Link to pdf

输出结果为:

*** Welcome to the Connect Four game!!! ***
Would you like to make the first move [y/n]: y
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
  1   2   3   4   5   6   7 
Please enter your move: 4
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   | O |   |   |   |
+---+---+---+---+---+---+---+
  1   2   3   4   5   6   7 
Computer moved in column: 1
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+
| X |   |   | O |   |   |   |
+---+---+---+---+---+---+---+
  1   2   3   4   5   6   7 
row = -355898527
Segmentation fault: 11

(如果您对上下文或目的有任何疑问,请随时询问!)

3 个答案:

答案 0 :(得分:4)

没有实际运行代码,我相信问题可能是在check_winner中没有初始化count:

int check_winner(int board[][BOARD_SIZE_VERT], int last_move)
{
    int i, row, count;

    // Find row
    for (row = 0; row < BOARD_SIZE_VERT; row++)
    {
        if (board[last_move-1][row]) count++;
    }
    row = count;
// WHAT IF IT EXITS THE for () WITHOUT board[last_move-1][row] being non-zero?
// HINT: count can be anything!
    printf ("row = %d\n", row);

然后使用row作为数组的访问权限,并繁荣!您已经访问了权限之外的内存。

答案 1 :(得分:1)

在check_winner函数中,您不会初始化变量计数。我得到一个unitialised变量运行时错误,但是如果我继续前面几行,那么你设置:

export default ({title, icon, ...remainingProps}) => (
  <TouchableOpacity {...remainingProps} style={styles.button}>
    {title && <Text style={styles.text}>{title}</Text>}
    {icon && <Icon name={icon} />}
 </TouchableOpacity>
);

在我的情况下,将行设置为-858993459;

你写的同一个函数中的几行:

row = count;

这就是我得到的地方:

if (board[last_move - 1][i] == board[last_move - 1][row]) count++; 
// You use "row", an initialised variable as the index
// for the second subscript operator of board.

我认为应该有一个关于这个整合变量的警告。老实说,我不知道这是不是错了,但它看起来是一个非常好的游戏。

答案 2 :(得分:0)

首先,为了能够解决您的问题,您应该确保在崩溃之前打印的所有内容都已打印出来。在此目标中,您应在每条打印指令后添加fflush(stdout);指令

其次,如果这不清楚你编程崩溃的地方,你应该在代码段中添加补充跟踪指令。

然后,当您确定哪个指令导致崩溃时,您应该打印它的变量以检查它们的值是否符合您的预期。

或者,您可以使用assert指令确保变量值都在预期范围内。