2015-04-05 60 views
0

我试图通过使用gdb和“dissassemble”来调试它,但它不显示行,只是它获取分段错误的函数。函数返回静态数组上的Seg错误

这里是代码:

应该检查,如果玩家获胜(他是最后一个活着),或丢失(他是不是在地图上了)

int * win_lose_verif(int number_of_player, int your_player); 

它是主循环,我需要它停止每当玩家赢得或失去

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; 
} 

这是我得到的gdb回溯后:

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() 

我不明白为什么我得到段错误。

我没有得到一个段错误的前20秒,但在那之后,我很可能得到它。为什么?

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

我改变了静态到的malloc

+0

编译-g,在gdb设置断点,检查一些变量,并测试你的假设 – 2015-04-05 20:31:09

+0

的malloc的condition_win_lose阵列,而不是试图返回一个静态的范围数组。 – 2015-04-05 20:48:07

+0

如果使用gcc作为编译器/链接器,则使用参数-ggdb。这会将最大量的调试信息放入可执行文件中。 -g只是简单的调试信息。 -ggdb添加gdb可以使用的额外信息。 – user3629249 2015-04-05 20:56:23

回答

0

通过减慢的过程(在while循环睡眠),赛格故障停止。

我可以推测我在无限循环中要求太多的操作,并造成了一些溢出。

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

这里是最后的:

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);