递归正女王程序获得C ++分段错误 [英] Getting C++ Segmentation Fault error in recursive n-queen program

查看:124
本文介绍了递归正女王程序获得C ++分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有写,解决了正女王的问题,这是我做了一个基本的程序,但它抛出一个分段错误,如果我输入任何数字> = 11。

Basically, I have to write a basic program that solves the n-queen problem, which I have done, but it throws a segmentation fault if I input any number >=11.

从我在线阅读,这个错误通常是由错误的逻辑与内存打交道时造成的,但我似乎无法找出什么我做错了。

From what I have read online, this error is usually caused by faulty logic when dealing with memory, but I can't seem to figure out what I have done wrong.

void generateBoard(int board[],int column,int length,int count)
{
    if(column == 0 && board[0]<length)  //prevents outputting the results infinitely 
    {
        ++board[0];
        generateBoard(board, ++column, length, count);
    } 
    else 
    {
        bool lineNotFound = true;
        int row = board[column];
        while(lineNotFound && row < length)
        {
            ++row; //temporary value for a column value candidate
            lineNotFound = false;
            for(int i = 0; i < column && !lineNotFound; ++i)
            {
                if(board[i] == row || (board[i]+column-i) == row || (board[i]-column+i) == row) // check diagonal and horizontal
                {
                    lineNotFound = true;
                }
                else
                {
                    board[column] = row;
                }
            }
        }
        if(column == length-1 && !lineNotFound) // at last column and valid board
        {
            output(board,length,++count);
            generateBoard(board,column,length,count);
        }
        else if(!lineNotFound) // not at last column, but valid position found
        {
            generateBoard(board,++column,length,count);
        }
        else if(column != 0) //no valid columns, go back a step
        {
            board[column] = 0;
            generateBoard(board,--column,length,count);
        }
    }
}

我意识到这是code的一大块​​,但我觉得有必要将它张贴所有获得问题的想法。

I realize that is a big chunk of code, but I think it's necessary to post it all to get an idea of the problem.

任何想法? :■

我是新来编程的C ++,所以我不知道从哪里开始调试这个。

I'm new to programming c++, so I don't know where to start debugging this.

推荐答案

创业之初调试,然后让它运行,直到分段错误。当故障发生时 - 看堆栈。我猜想,你是超出您的堆栈递归的 - 这是最主要的问题递归程序

Start debugging at the beginning and then let it run till the segmentation fault. When the fault occurs - look at the stack. I would guess that you're exceeding your stack with the recursion - that's the main problem with recursion programs.

您可以放大你的筹码,然后将不会出现与输入11,但与其他一些数字的错,而是递归程序总是会在堆栈崩溃的问题与输入足够大。

You can enlarge your stack and then the fault will occur not with input 11 but with some other number, but recursive programs will always crash on stack issues with the input large enough.

顺便说一句 - 确保您的递归是有界的,即:在某些时候你的函数应该没有进一步自称退出。恕我直言,这是最好是在开始时进行(虽然它的风格和方便的事),因为那时你会看到清晰的条件赖以递归结束,它会更容易调试无穷递归(这也导致赛格故障因为堆耗尽)。你的情况是不立即清除递归如何结束,我不会感到惊讶,它并不适用于某些输入。

By the way - make sure your recursion is bounded, i.e.: at some point your function should exit without calling itself further. IMHO, this is better be done at the beginning (although its a matter of style and convenience), because then you'll see clearly the condition upon which the recursion ends and it will be easier to debug infinite recursions (which also cause seg faults because of stack exhaustion). In your case it is not immediately clear how the recursion ends, and I wouldn't be surprised that it doesn't for some inputs.

澄清

虽然在某些系统上,你得到一个堆栈溢出的错误,别人就会得到段错误同样的事情。我猜你的他者之一。

While on some systems you get a "Stack Overflow" error, on others you would get "Segmentation Fault" for the same thing. I'm guessing you're on one of the "others".

要表明,我只是编译并运行此code:

To show that, I just compiled and ran this code:

int foo(long a)
{
  return foo(a-1);
}


int main()
{
   return foo(9999999999L);
}

在我的GCC / Ububtu机。这个程序有无限递归,与结束分段故障崩溃。

On my GCC/Ububtu machine. This program has infinite recursion, which ends up with "segmentation fault" crash.

您可以将任何递归算法转换为迭代。取而代之的递归调用新的变量函数,使用STL 的std ::栈来推动和弹出他们在一个循环中运行。这里的细节:<一href=\"http://www.cplusplus.com/reference/stl/stack/\">http://www.cplusplus.com/reference/stl/stack/

You can convert any recursive algorithm into iterative. Instead of recursively calling the function with new variables, use the STL std::stack to push and pop them and run in a loop. Here the details : http://www.cplusplus.com/reference/stl/stack/

这篇关于递归正女王程序获得C ++分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆