处理随机数时堆栈溢出/段错误
我正在创建一个小井字棋游戏,并使用此函数随机生成计算机的移动:
//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
创建二维数组的代码:
char** tictactoe::createBoard() { //将其初始化为某个东西 炭**板;
//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;
}
I'm creating a little tic tac toe game and I generate the computer's move randomly using this function:
//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 );
}
That function is called in a loop immediately after the human user makes their move. The reason I think this is the issue is because the message I get is dealing with randoms and this is the only place I use them. I have these files included:
#include "tictactoe.h" //Homemade header file
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>
The error message generated by valgrind looks like this:
==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
Code to create the 2d array:
char** tictactoe::createBoard()
{
//Initialize it to something
char **board;
//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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的 mod 操作
rand() % 2
只会输出 0 或 1,你应该使用rand() % 3
。可能发生的情况是,您只是随机探索整个棋盘的 2x2 子矩阵。Your mod operation
rand() % 2
will only output a 0 or a 1, you should userand() % 3
. What is probably happening is that you are only randomly exploring a 2x2 sub-matrix of the whole board.不。当板(准确地说:2x2 上部,因为
%2
只会输出 0 和 1)已满时,它就会崩溃:在这种情况下没有退出条件。相反,您会调用getComputerMove()
直到堆栈已满:堆栈溢出。Nope. It just crashes when the board (to be exact: the 2x2 upper part, as
%2
will output only 0 and 1) is full: There is no exit condition in that case. Instead you callgetComputerMove()
until the stack is full: Stack overflow.