Segfault在节目结束时

时间:2014-10-07 03:53:47

标签: c++ pointers segmentation-fault

我把这个程序写成了我的一位教授的副课程。它从给定文本文件中的9x9块读入数独谜题,然后使用递归的回溯链表明确地解决它。这样做很好,但我总是在执行结束时遇到段错误。

#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int ctoi(char c);
void printBoard();
int board[9][9];

class Node
{
    public:
    int x_, y_;
    int value;
    Node* next_;
    bool checkAllowed();
    bool processNextNode();
    bool requiredValue;
};

bool Node::checkAllowed()
{

    int checkVal = value;
    //Check vertically
    for(int i = 0; i < 9; i++)
    {
        if(i != y_)
        {
            int compareVal = board[x_][i];
            if(board[x_][i] == value)
            {
                return false;
            }
        }
    }

    //Check horizontally
    for(int i = 0; i < 9; i++)
    {
        if(i != x_)
        {
            if(board[i][y_] == value)
            {
                return false;
            }
        }
    }

    //Check 3x3 block
    int offsetX = x_ - x_ % 3;
    int offsetY = y_ - y_ % 3;

    for (int i = 0; i < 9; i++)
    {
        int x, y;
        x = offsetX + i % 3;
        y = offsetY + i / 3;
        if (x != x_ && y != y_)
        {
            if(board[x][y] == value)
            {
                return false;
            }
        }
    }
    return true;
}

bool Node::processNextNode()
{
    if(requiredValue)
    {
        if(next_ == 0)
        {
            return true;
        }
        else
        {
            return next_ -> processNextNode();
        }
    }
    for(int i = 1; i <= 9; i++)
    {
        value = i;
        if(checkAllowed())
        {
            board[x_][y_] = value;
            printBoard();
            if(next_ == 0)
            {
                return true;
            }
            else if(next_ -> processNextNode())
            {
                return true;
            }
        }
    }
    return false;
}

int main()
{
    char* fileName;
    ifstream fin;
    Node* head;
    Node* next = new Node();
    head = next;

    cout << "Enter file name : ";
    cin >> fileName;

    fin.open(fileName);

    if(fin == 0)
    {
        cout << "File does not exist" << endl;
        return 1;
    }

    for(int i = 0; i < 9; i++)
        {
        char* line;
        fin >> line;
        for(int j = 0; j < 9; j++)
        {
            board[j][i] = ctoi(line[j]);
            if(board[j][i] == 0)
            {
                next -> requiredValue = false;
            }
            else
            {
                next -> requiredValue = true;
            }
            next -> x_ = j;
            next -> y_ = i;
            next -> value = board[j][i];
            if(i == 8 && j == 8)
            {
                next -> next_ = 0;
            }
            else
            {
                next -> next_ = new Node;
                next = next -> next_;
            }
        }
    }

    printBoard();

    bool good = head -> processNextNode();

    printBoard();
    if(!good)
    {
        cout << "Puzzle is unsolveable." << endl;
    }

    return 0;
}

int ctoi(char c)
{
    if(c == '.')
    {
        return 0;
    }
    return (int) c - (int) '1' + 1;
}

void printBoard()
{
    system("cls");
    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < 9; j++)
        {
            cout << board[j][i];
        }
        cout << endl;
    }
}

我是c ++指针的新手,我认为这可能是某些元素删除不当的结果。任何见解都将不胜感激。

1 个答案:

答案 0 :(得分:2)

这行代码:

cin >> fileName;

表示&#34;从标准输入读取一个字符串,并将该字符串的内容放在fileName指向的位置。&#34;不幸的是,fileName是一个未初始化的指针,因此它会覆盖内存中的随机字节。

由于它等待进程退出到segfault,所以被覆盖的内容似乎是进程清理处理程序的一部分。

解决此问题的最简单方法是改为:

#include <string>
...
std::string fileName;
...
cin >> fileName;
fin.open(fileName);
相关问题