简体   繁体   English

使用递归函数时出现分段错误

[英]Segmentation fault when using recursion function

I'm quite new to programming and for my Object Oriented class (in C++), we have a 2d array with random groupings of "X".我对编程很陌生,对于我的面向对象类(在 C++ 中),我们有一个带有“X”随机分组的二维数组。 I have to use a recursive function to find the different groupings and clear them.我必须使用递归函数来查找不同的分组并清除它们。 As of right now, I check if the spot is an X, clear it, then check the 8 positions around it (including diagonals), and if one of the positions in an X, I call the function again but on that location.到目前为止,我检查该点是否是 X,清除它,然后检查它周围的 8 个位置(包括对角线),如果 X 中的一个位置,我再次调用该函数,但在该位置。 My idea is that if I find one X, I will be able to get all the X's around it in one go, thus, I can count it as a group when I find an X.我的想法是,如果我找到一个X,我就可以一口气把它周围的所有X都找出来,因此,当我找到一个X时,我可以把它算作一个组。

At the end of the function, I basically loop through all the spots and call the recursive function again if there is another X. However, I keep getting segmentation faults and I'm not sure why.在函数的末尾,我基本上循环遍历所有的点,如果还有另一个 X,我会再次调用递归函数。但是,我不断收到分段错误,我不知道为什么。 Any help would be greatly appreciated!任何帮助将不胜感激!


void  Recurssive(string Array[][72],int Pos1, int Pos2)
{

int One=1;
int Two=1;
//cout<<"Test 2";
if(Array[Pos1][Pos2]=="X")
        {
        Array[Pos1][Pos2]="0";
        if(Array[Pos1+1][Pos2]=="X")
                {
                Recurssive(Array,Pos1+1,Pos2);
                }
         if(Array[Pos1-1][Pos2]=="X")
                {
                Recurssive(Array,Pos1-1,Pos2);
                }
         if(Array[Pos1][Pos2+1]=="X")
                {
                Recurssive(Array,Pos1,Pos2+1);
                }
         if(Array[Pos1][Pos2-1]=="X")
                {
                Recurssive(Array,Pos1,Pos2-1);
                }
         if(Array[Pos1+1][Pos2+1]=="X")
                {
                Recurssive(Array,Pos1+1,Pos2+1);
                }
         if(Array[Pos1-1][Pos2-1]=="X")
                {
                Recurssive(Array,Pos1-1,Pos2-1);
                }
         if(Array[Pos1+1][Pos2-1]=="X")
                {
                Recurssive(Array,Pos1+1,Pos2-1);
                }
         if(Array[Pos1-1][Pos2+1]=="X")
                {
                Recurssive(Array,Pos1-1,Pos2+1);
                }

        }

for(int i=1;i<22;i++)
        {
        for(int j=1;j<72;j++)
                {
                if(Array[i][j]=="X")
                        {
                        Recurssive(Array,i,j);
                        }
                }
        }


}

Here is the output of the array I am looping through这是我循环遍历的数组的输出

            X                                                         
             X                                                        
              X           XXXXXXXXXXXXXXX                             
               X          XXXXXXXXXXXXXXX                             
                X         XXXXXXXXXXXXXXX        XXXX                 
                 XXXX     XXXXXXXXXXXXXXX      XXX  XXX               
                     X                        XXX    XXX              
     XXXXXXXXXXXXXX   X                      XXX      XXX             
     XX          XX    X                      XXX    XXX              
     XX          XX     X                      XXX  XXX               
     XX          XX      X                       XXXX                 
     XX  XXXXX   XX       X                                           
     XX          XX        X                                          
     XX          XX         X                                         
     XXXXXXXXXXXXXX          X                                        
                                          X                           
                                         X                            
                                        X                             
                                       X                              
                                      X 

Let's play computer and go through the motions for Recurssive(Array, 0, 0) .让我们玩电脑并完成Recurssive(Array, 0, 0) If this position was marked as X , this will make the following array accesses, in order:如果这个位置被标记为X ,这将按顺序进行以下数组访问:

Array[1][0]
Array[-1][0]
Array[0][1]
Array[0][-1]
Array[1][1]
Array[-1][-1]
Array[1][-1]
Array[-1][1]

These -1 accesses will go outside the memory defined by Array and may read a random value or may cause a segmentation fault.这些-1访问将超出Array定义的内存,并可能读取随机值或可能导致分段错误。

In order to fix this, you need to verify that a candidate position is inside the board before you actually access it.为了解决这个问题,您需要在实际访问之前验证候选位置是否在板内。 Something like:就像是:

std::optional<std::string> safe_access(std::string Array[][72], int Pos1, int Pos2) {
  if (Pos1 < 0 || Pos1 >= 71) return {};
  if (Pos2 < 0 || Pos1 >= 21) return {};
  return Array[Pos1][Pos2];
}

You can then call safe_access(Array, -1, -1) == "X" and be guaranteed this will not access outside of Array .然后您可以调用safe_access(Array, -1, -1) == "X"并保证这不会在Array之外访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM