C++ Cpp-递归中的EXC\u坏访问

C++ Cpp-递归中的EXC\u坏访问,c++,recursion,vector,C++,Recursion,Vector,我写了一个递归函数,它使用std::vector作为临时数据容器。问题在于,在某些递归迭代之后,代码抛出EXC\u BAD\u ACCESS错误,这些错误似乎与代码无关,因为异常会四处传播 我可以产生一个异常如果我在代码printf的位置放置任何东西(“这很有趣…不是”),之前的任何代码似乎都可以执行 我怀疑我的系统内存不足,但从活动监视器中我可以看到我使用了6.5/8GB RAM,所以情况不会是这样 此EXC\u BAD\u访问异常可能是什么情况 void Terrain::CarvePath

我写了一个递归函数,它使用std::vector作为临时数据容器。问题在于,在某些递归迭代之后,代码抛出EXC\u BAD\u ACCESS错误,这些错误似乎与代码无关,因为异常会四处传播

我可以产生一个异常如果我在代码printf的位置放置任何东西(“这很有趣…不是”),之前的任何代码似乎都可以执行


我怀疑我的系统内存不足,但从活动监视器中我可以看到我使用了6.5/8GB RAM,所以情况不会是这样

此EXC\u BAD\u访问异常可能是什么情况

void Terrain::CarvePath(int index){

    float endElevation = 0.0f;

    int actualRowSize = 25 + 1;

    int upperRow = index - actualRowSize;

    int bottomRow = index + actualRowSize;

    if(bottomRow + 1 > 25 * 25 || upperRow - 1 < 0){
        return;
    }

    std::vector<int> surroundingIndices;

    // Throws EXC_BAD_ACCESS. If removed throws EXC_BAD_ACCESS 
    // on the next line ( surroundingIndices.push_back() )
    printf("This is funny ... not");

    surroundingIndices.push_back(upperRow - 1);
    surroundingIndices.push_back(upperRow);
    surroundingIndices.push_back(upperRow + 1);
    surroundingIndices.push_back(index - 1);
    surroundingIndices.push_back(index + 1);
    surroundingIndices.push_back(bottomRow - 1);
    surroundingIndices.push_back(bottomRow);
    surroundingIndices.push_back(bottomRow + 1);



    if(lastVertex){
       std::remove(std::begin(surroundingIndices), 
       std::end(surroundingIndices), lastVertex);
    }

    std::vector<float> surroundingIndicesY;

    for (auto &&surroundingIndex : surroundingIndices) {
         surroundingIndicesY.push_back
         (vertices[surroundingIndex].position.y);
    }

    std::vector<float>::iterator it;

    it = std::min_element(surroundingIndicesY.begin(), 
    surroundingIndicesY.end());

    long vertexToDigIndexTemp = 
    std::distance(surroundingIndicesY.begin(), it);

    int vertexToDigIndex = surroundingIndices[vertexToDigIndexTemp];

    vertices[vertexToDigIndex].position.y -= 1.0f;

    lastVertex = vertexToDigIndex;

    if(vertices[index].position.y == endElevation) return;

    CarvePath(vertexToDigIndex);

}
void地形::CarvePath(int索引){
浮动高度=0.0f;
int actualRowSize=25+1;
int upperRow=索引-实际Rowsize;
int bottomRow=索引+实际rowsize;
如果(下排+1>25*25 | |上排-1<0){
返回;
}
std::矢量环绕骰子;
//抛出EXC\u坏访问。如果删除,抛出EXC\u坏访问
//在下一行(包围骰子。向后推())
printf(“这很有趣……不是”);
环绕骰子。推回(上排-1);
环绕骰子。向后推(上排);
环绕骰子。向后推(上排+1);
环绕骰子。向后推(索引-1);
环绕骰子。向后推(索引+1);
环绕骰子。向后推_(底部行-1);
环绕骰子。向后推(最底层);
环绕骰子。向后推_(底部行+1);
if(lastVertex){
标准::移除(标准::开始(环绕骰子),
std::end(环绕骰子),最后一个顶点;
}
std::矢量环绕;
用于(自动和环绕索引:环绕骰子){
把你推回去
(顶点[surroundingIndex].position.y);
}
std::vector::it迭代器;
it=std::min_元素(包围dicesy.begin(),
环绕dicesy.end());
长vertexToDigIndexTemp=
std::距离(环绕dicesy.begin(),it);
int vertexToDigIndex=环绕DICES[vertexToDigIndexTemp];
顶点[顶点索引].position.y-=1.0f;
lastVertex=顶点索引;
if(顶点[index].position.y==Endelvation)返回;
路径(顶点索引);
}

在我看来就像是典型的堆栈溢出。由于代码是递归的,因此堆栈上可能会有很多返回地址,这可能会导致堆栈空间不足。将代码转换为非递归版本可以解决此问题。

“我可以看出我使用了6.5/8GB RAM,所以情况并非如此?”是,如果分配器不能分配一个足够大的连续块,则可能会发生这种情况。可能是其他地方未定义的内容–对行为产生影响的微小输出更改通常就是一种迹象。首先使用调试器找出发生时您试图访问的内容,以及递归的深度。@NeilButterworth这是在将
int
推入空向量时,我认为这更有可能抛出
std::bad_alloc
。代码在中断之前执行了2472次。。。问题在于(顶点[index].position.y==Endelvation)返回的条件;不会计算为true,因此递归将无限循环您的代码不完整;特别是,它似乎缺少一个
main()
函数和至少一个
#include
。请输入您的代码,使其成为您问题的一部分,然后我们可以尝试复制并解决它。您还应该阅读。实际上,这是由于我在处理递归函数结尾时的粗心大意造成的堆栈溢出,更改递归的条件出口解决了问题。