2012-02-18 130 views
1

我创建一个小的井字游戏,我随意使用此功能生成计算机的举动:堆栈溢出/ SEG故障

//Generates the computer's move randomly 
void tictactoe::getComputerMove(char** b) 
{ 
    int rand1,rand2; 

    //Generate two random numbers 
    rand1 = rand() % 2;//0-2 
    rand2 = rand() % 2;//0-2 

    //Check if that spot is taken yet. 
    if(b[rand1][rand2] == ' ')//Empty 
      b[rand1][rand2] = 'O'; 

    else //Already filled. 
     getComputerMove(b); 
} 

该函数调用后立即在一环人类用户进行移动。我认为这是问题的原因是因为我得到的消息是处理随机数,这是我使用它们的唯一地方。我已经包含这些文件:

#include "tictactoe.h" //Homemade header file 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <string> 
#include <iostream> 

通过的valgrind产生的错误信息是这样的:

==31280== Stack overflow in thread 1: can't grow stack to 0xbe398ff8 
==31280== 
==31280== Process terminating with default action of signal 11 (SIGSEGV) 
==31280== Access not within mapped region at address 0xBE398FF8 
==31280== at 0x41AB4BF: random_r (random_r.c:364) 
==31280== If you believe this happened as a result of a stack 
==31280== overflow in your program's main thread (unlikely but 
==31280== possible), you can try to increase the size of the 
==31280== main thread stack using the --main-stacksize= flag. 
==31280== The main thread stack size used in this run was 8388608. 
==31280== Stack overflow in thread 1: can't grow stack to 0xbe398ff4 
==31280== 
==31280== Process terminating with default action of signal 11 (SIGSEGV) 
==31280== Access not within mapped region at address 0xBE398FF4 
==31280== at 0x4021430: _vgnU_freeres (vg_preloaded.c:58) 
==31280== If you believe this happened as a result of a stack 
==31280== overflow in your program's main thread (unlikely but 
==31280== possible), you can try to increase the size of the 
==31280== main thread stack using the --main-stacksize= flag. 
==31280== The main thread stack size used in this run was 8388608. 
==31280== 
==31280== HEAP SUMMARY: 
==31280==  in use at exit: 21 bytes in 4 blocks 
==31280== total heap usage: 4 allocs, 0 frees, 21 bytes allocated 
==31280== 
==31280== LEAK SUMMARY: 
==31280== definitely lost: 0 bytes in 0 blocks 
==31280== indirectly lost: 0 bytes in 0 blocks 
==31280==  possibly lost: 0 bytes in 0 blocks 
==31280== still reachable: 21 bytes in 4 blocks 
==31280==   suppressed: 0 bytes in 0 blocks 
==31280== Rerun with --leak-check=full to see details of leaked memory 
==31280== 
==31280== For counts of detected and suppressed errors, rerun with: -v 
==31280== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 20 from 9) 
Segmentation fault 

代码来创建二维数组:

字符** tictactoe :: createBoard() { //将其初始化为 char **板;

//Allocate memory for the array 
    board = new char* [3]; 

    for(int i = 0; i < 3; i++) 
    { 
     board[i] = new char[3]; 
    } 

    //Now fill each slot with a dummy ' ' 
    for(int i = 0; i < 3; i++) 
    { 
     for(int j = 0; j < 3; j++) 
      board[i][j] = ' '; 
    } 

    return board; 
} 
+0

如何分配数组b的内存? – 2012-02-18 01:02:47

+0

我会将代码添加到主帖子中。 – Joshua 2012-02-18 01:07:17

回答

2

你的MOD操作rand() % 2只能输出一个0或1,你应该使用rand() % 3。可能发生的事情是,你只是随机地探索整个电路板的2x2子矩阵。

+0

那肯定是可以的:x – Joshua 2012-02-18 01:06:56

+0

就是这样,谢谢 – Joshua 2012-02-18 02:50:33

1

究其原因,我认为这是问题,因为我得到的消息是处理随机量

都能跟得上。当板(确切地说:2x2上部,因为%2只输出0和1)已满时,它只是崩溃:在这种情况下没有退出条件。相反,您可以拨打getComputerMove(),直到堆栈满:堆栈溢出。