C 返回静态阵列功能中的Seg故障

C 返回静态阵列功能中的Seg故障,c,segmentation-fault,gdb,C,Segmentation Fault,Gdb,我试着用gdb和“disassemble”来调试它,但它没有显示行,只是显示了它出错的函数 代码如下: 它应该检查玩家是赢了(他是最后一个活着的),还是输了(他不再在地图上了) 这是主循环,我需要它在玩家赢或输的时候停止 int network_fn(int number_of_player, int your_player); 完整代码: int * win_lose_verif(int number_of_player,int your_player) { int i; i

我试着用gdb和“disassemble”来调试它,但它没有显示行,只是显示了它出错的函数

代码如下:

它应该检查玩家是赢了(他是最后一个活着的),还是输了(他不再在地图上了)

这是主循环,我需要它在玩家赢或输的时候停止

int network_fn(int number_of_player, int your_player);
完整代码:

int * win_lose_verif(int number_of_player,int your_player) {
    int i;
    int j;
    int is_our_player_alive=0;
    int are_the_other_alive=0;
    int* condition_win_lose; //[0]lose condition [1]win condition

    condition_win_lose=(int*) malloc(sizeof(int)*2);        

    for (i=0; i<end_of_map_x-1; ++i) 
    {
        for (j=0; j<end_of_map_y-1; ++j) 
        {
            if (map[i][j] && (map[i][j]->species==P1||map[i][j]->species==P2||map[i][j]->species==P3||map[i][j]->species==P4) ) 
            {
                if(your_player==map[i][j]->species) //our player is here
                    is_our_player_alive++;
                else
                    are_the_other_alive++;
            }
        }
    }

    if(is_our_player_alive==0) {
        condition_win_lose[0]=1;
    }
    else {
        condition_win_lose[0]=0;
        condition_win_lose[1]=0;
        if(are_the_other_alive==0)
        {    condition_win_lose[1]=1;      }
    }       

    return condition_win_lose;

}



int network_fn(int number_of_player, int your_player) {
    int tick=0;
    int lose=0;
    int win=0;
    int* check_win_lose; // *(check_win_lose+0) for lose , +1 for win

    do {
        check_win_lose=win_lose_verif(number_of_player,your_player);

        if (*(check_win_lose + 0)==1) {
            return ENDGAME;
        }
        else if (*(check_win_lose + 1)==1) {
            return ENDWIN;
        }

    } while (1);

    return EXIT_SUCCESS;
}
我不明白为什么我会犯错误

我在前20秒没有遇到故障,但在那之后,我很可能会遇到故障。为什么?

编辑:对于似乎不清楚的评论。我试着用ggdb编译,但仍然没有得到更多的信息。 如何使gdb打印SEGFULT的行


我通过减慢进程(while循环中的睡眠)将静态更改为malloc,seg故障停止。

我可以假设我在一个无限循环中请求了太多的操作,并产生了一些溢出

我不知道这是否是最好的答案,但这对我来说很好。请注意,如果您要求太多的操作,无限循环可能非常棘手

这是最后的结果:

   do {
        sleep(1);
        check_win_lose=win_lose_verif(number_of_player,your_player);

        if (*(check_win_lose + 0)==1) {
            return ENDGAME;
        }
        else if (*(check_win_lose + 1)==1) {
            return ENDWIN;
        }

    } while (1);

使用-g编译,在gdb中设置断点,检查一些变量,并测试您的假设smalloc条件\u win\u lose数组,而不是尝试返回静态作用域数组。如果使用gcc作为编译器/链接器,则使用参数-ggdb。这将在可执行文件中放入最大数量的调试信息-g只是简单的调试信息-ggdb添加了gdb可以使用的额外信息0代表输,1代表赢”与以下代码的操作不匹配数组检查\u win\u lose[]只能指示当前玩家是否不再在游戏板上。它并不表示当前玩家获胜,除非棋盘上所有其他玩家都失踪。发布的代码没有实现该逻辑
Program received signal SIGSEGV, Segmentation fault.
0x0000000000403231 in win_lose_verif ()
(gdb) backtrace
#0  0x0000000000403231 in win_lose_verif ()
#1  0x0000000000403cff in network_fn ()
#2  0x0000000000400db0 in main ()
   do {
        sleep(1);
        check_win_lose=win_lose_verif(number_of_player,your_player);

        if (*(check_win_lose + 0)==1) {
            return ENDGAME;
        }
        else if (*(check_win_lose + 1)==1) {
            return ENDWIN;
        }

    } while (1);