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

时间:2015-10-28 18:55:05

标签: c++ segmentation-fault

我正在努力解决一个难题并且进展顺利一段时间。我习惯于获得分段错误但我已经克服了它们直到这个。因为在这一个中,我没有指出指向可能发生分段错误的位置。我使用了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 );
}

因为我可以输出&#39; tempArr [i]&#39;对于本地&#39; i&#39;第一次= 0,然后事情变得混乱。但实在看不出哪条线是有问题的。还有一个问题,那就是有限的记忆相关问题吗?

感谢。

1 个答案:

答案 0 :(得分:1)

startBusiness功能中,您有

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

首先调用checkAtla,然后检查范围。因此,您使用checkAtla致电i==boardSize-2并导致段错误。