【问题标题】:Running into an identifiable Segmentation Fault遇到可识别的分段错误
【发布时间】:2018-03-09 02:36:59
【问题描述】:

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

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

为我的代码提供一些上下文:该程序的目标是实现计算机和人类玩家之间的四人连接游戏。函数将在文件 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

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

【问题讨论】:

  • 在给你一个错误之前它会打印任何东西吗?如果是这样,你能给我们在段错误之前和包括段错误之前的完整输出吗?
  • if (getchar() == 'N' || getchar() == 'n') return 2; 可能会获取包机两次。一个电话getchar() 就足够了。
  • 要审查的代码很多,你能缩小范围吗?你到底什么时候收到段错误?
  • 抱歉,我在发帖前忘记添加了。我现在正在添加它。感谢您提请我注意!
  • Segmentation fault 从您添加的标签中可以清楚地看出,因此在问题标题中没有用。 (请帮助) 是无用的噪音,因为您在这里提出问题的事实表明您需要帮助。您应该做的第一件事(在编辑标题以使其具有在搜索结果列表中看到时有用的某些含义之后)是学习使用调试器逐步执行代码以缩小问题区域。然后专注于该代码以隔离代码的特定区域,然后您可以使用它来编写您可以在此处发布的minimal reproducible example

标签: c segmentation-fault runtime-error


【解决方案1】:

没有实际运行代码,我认为问题可能是在 check_winner 中未初始化计数:

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 作为对数组的访问,然后砰!您访问了超出您权限的内存。

【讨论】:

    【解决方案2】:

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

    row = count;
    

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

    在您编写的同一个函数中的几行代码:

    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.
    

    这就是我得到的:

    Access violation reading location 0x0000005663E82BB4. occurred
    

    我认为应该显示有关此未初始化变量的警告。老实说,我不知道这是否是问题的全部,但它看起来是一款相当不错的游戏。

    【讨论】:

      【解决方案3】:

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

      其次,如果这不能说明你程序崩溃的地方,你应该在它发生的那段代码中添加补充跟踪指令。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-05
        • 2017-08-20
        • 1970-01-01
        • 2022-01-02
        相关资源
        最近更新 更多