分段错误,没有详细的编译警告

Segmentation fault with no detailed compiling warnings

本文关键字:编译 警告 错误 分段      更新时间:2023-10-16

我正在努力解决一个难题,有一段时间进展顺利。我习惯于遇到分割错误,但我已经克服了它们,直到这次。因为在这篇文章中,我没有得到任何指示,指出分段错误可能发生在哪里。我使用了Dev C++和MS Visual studio Community 2015,但他们都没有向我提供任何关于这一行的信息,这是错误的来源。

我正在分享下面的整个代码,然后我将分享我认为问题来源最重要的特定功能块:

#include <iostream>
#include <vector>
int boardSize, result=0;
int * board;
std::vector<std::vector<int> > tempVec;
void askSize()
{
    //
    std::cout << "type in the size of the boardSize: "<< std::endl;
    std::cin >> boardSize;
    std::cout << std::endl;
    board = new int [boardSize];
    for(int i = 0; i < boardSize; i++)
    {
        //
        if( i < boardSize/2 )
        {
            //
            //std::cout<< i;
            board[i] = 1;
        }
        else
        {
            board[i] = 0;
        }
    }
    for( int i = 0; i < boardSize; i++ )
    {
        //
        std::cout << *(board + i) <<std::endl;
    }
    std::cout << std::endl;
}

void showBoard()
{
    //
    for( int i = 0; i < boardSize; i++ )
    {
        //
        std::cout << *(board + i) << ", " << std::endl;
    }
    std::cout << std::endl;
}

int git(int * arrX, int x)
{
    //
    if( arrX[x+1] == 0 )
    {
        //
        arrX[x] = 0;
        arrX[x+1] = 1;
        return 1;
    }
    else
    {
        return 0;
    }
}
int checkGit(int * arrX, int x)
{
    //
    if( arrX[x+1] == 0 )
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
void atla(int * arrX, int y, int jumpCount)
{
    //
    if( ( jumpCount > 0 && arrX[y+jumpCount*2+2] == 1 && y < boardSize-2 ) || ( jumpCount > 0 && arrX[y+jumpCount*2+1] == 0 && y < boardSize-2 ) )
    {
        //
        arrX[y] = 0;
        arrX[y + jumpCount*2] = 1;
        //return jumpCount;
    }
    else if( jumpCount > 0 && arrX[y+jumpCount*2+2] == 0 && y < boardSize-2 && arrX[y+jumpCount*2+1] == 1)
    {
        //
        atla(arrX, y, jumpCount+1);
    }
    else if( jumpCount == 0 && arrX[y+2] == 0 && arrX[y+1] == 1)
    {
        //
        atla(arrX, y, jumpCount+1);
    }
    else if( ( jumpCount == 0 && arrX[y+2] == 1 ) || ( jumpCount == 0 && arrX[y+1] == 0 ) )
    {
        //
        //return 0;
    }
}
int checkBoard(int * arrX)
{
    //
    for( int i = 0; i < boardSize; i++ )
    {
        //
        if( i < boardSize/2 )
        {
            //
            if( arrX[i] == 1 )
            {
                //
                return 0;
            }
        }
        else
        {
            //
            if( arrX[i] == 0 )
            {
                //
                return 0;
            }
        }
    }
    return 1;
}
int checkAtla( int * arrX, int x )
{
    //
    if( arrX[x + 1] == 1 && arrX[x + 2] == 0 )
    {
        //
        return 1;
    }
    else
    {
        //
        return 0;
    }
}
void changeTempArr(int x, int y, int * arrX)
{
    //
    if( y == 0 )
    {
        //
        git(arrX, x);
    }
    else if( y == 1 )
    {
        //
        atla(arrX, x, 0);
    }
}
void startBusiness( int counter, int * arrX )
{
    //
    /*
    int * tempArr = new int [boardSize];
    for( int i = 0; i < boardSize; i++ )
    {
        //
        tempArr[i] = arrX[i];
    }
    */
    for( int i = 0; i < boardSize; i++ )
    {
        //
        if( checkAtla(arrX, i) == 1 && i < boardSize - 2 )
        {
            //
            tempVec.push_back({i,1});
        }
        if( checkGit(arrX, i) == 1 && i < boardSize - 1 )
        {
            //
            tempVec.push_back({i,0});
        }
    }
    if( tempVec.size() > 0 )
    {
        //
        int ** possibilities = new int * [tempVec.size()];
        for( int i = 0; i < tempVec.size(); i++ )
        {
            //
            possibilities[i] = new int[2];
            possibilities[i][0] = tempVec[i][0];
            possibilities[i][1] = tempVec[i][1];
        }
        int possibilitySize = tempVec.size();
        tempVec.clear();
        //std::cout<< possibilities[10][0] << ", " << possibilities[10][1];
        for( int i = 0; i < possibilitySize; i++ )
        {
            //
            int * tempArr = new int [boardSize];
            for( int i = 0; i < boardSize; i++ )
            {
                //
                tempArr[i] = arrX[i];
                //std::cout << tempArr[i] << std::endl;
            }
            changeTempArr(possibilities[i][0], possibilities[i][1], tempArr);
            //std::cout << "check" << std::endl;
            startBusiness( counter + 1, tempArr );
        }
    }
    else
    {
        //
        if( checkBoard(arrX) == 1 )
        {
            //
            if( counter > result)
            {
                //
                result = counter;
                std::cout << result << std::endl;
            }
        }
    }

}
int main()
{
    askSize();
    startBusiness(0,board);
    std::cout << "the result is: " << result;
    return 0;
}

我认为问题在这两行之间(在启动业务功能中):

int ** possibilities = new int * [tempVec.size()];
for( int i = 0; i < tempVec.size(); i++ )
{
    //
    possibilities[i] = new int[2];
    possibilities[i][0] = tempVec[i][0];
    possibilities[i][1] = tempVec[i][1];
}
int possibilitySize = tempVec.size();
tempVec.clear();
//std::cout<< possibilities[10][0] << ", " << possibilities[10][1];
for( int i = 0; i < possibilitySize; i++ )
{
    //
    int * tempArr = new int [boardSize];
    for( int i = 0; i < boardSize; i++ )
    {
        //
        tempArr[i] = arrX[i];
        //std::cout << tempArr[i] << std::endl;
    }
    changeTempArr(possibilities[i][0], possibilities[i][1], tempArr);
    //std::cout << "check" << std::endl;
    startBusiness( counter + 1, tempArr );
}

因为我第一次可以输出本地"I"=0的"tempArr[I]",然后事情变得一团糟。但真的看不出哪一条线是有问题的。还有另一个问题,这可能是与内存有限有关的问题吗?

谢谢。

startBusiness函数中,您有

if( checkAtla(arrX, i) == 1 && i < boardSize - 2 )
{
    //
    tempVec.push_back({i,1});
}

这将首先调用checkAtla,然后检查范围。因此,您使用i==boardSize-2调用checkAtla,将导致segfault。