由于递归分割故障 [英] Segmentation fault due to recursion

查看:156
本文介绍了由于递归分割故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个程序,它是取1-10之间的数字并显示安排数的所有可能的方式。

防爆
输入:3
输出:

  1 2 3
   1 3 2
   2 1 3
   2 3 1
   3 1 2
   3 2 1

每当我输入9或10,程序给出段故障,并转储核心。我相信这个问题是我的递推算法被称为太多次了。有人能帮忙指出,我怎么能限制递归调用所需的金额是多少?这是我目前的code:

 无效rearange(INT编号[11],INT指数,诠释NUM,诠释事实){  INT TEMP =号[指数]
  数[指数] =号[索引1];
  号码[索引1] =温度;  INT I;
  对于(i = 1; I< = NUM​​ ++ I)//打印当前序列
  {
    的printf(%D,数字[I]);
  }
  的printf(\\ n);  事实 - ; //减量多少序列保持
  指数 -​​ ; //递减我们的索引数组中  如果(指数== 1)//如果我们在数组的开始
    指数= NUM​​; //重置索引到阵列的端  如果(实际上大于0)//如果我们有更多的剩余序列
    rearange(数字,指标,NUM,事实); //再次做到这一切! :D
}诠释主(){
  INT NUM,我; //我们的数字和计数器  的printf(请输入小于10的数字:);
  scanf函数(%D,试验#); //从用户那里得到数  INT数字[11]; //创建适当大小的数组
  //填写阵列
  对于(i = 1; I< = NUM​​;我++){//填充数组从1至NUM
    号码[我] =我;
  }  INT事实= 1; //计算阶乘确定
  对于(i = 1; I< = NUM​​ ++ I)//有多少可能的序列
  {
    其实=其实我*;
  }  rearange(数字,NUM,NUM,事实); //首先递归清理  返回0;
}


解决方案

9!(362880)和 10 <!/ code >(3628800)是溢出的 调用堆栈 当你庞大的数字做尽可能多的递归调用。因为所有的局部变量和形式参数必须被存储。你要么就得增加堆栈大小或递归转换成迭代。

在Linux中,你可以这样做:

 的ulimit -s无限

堆栈大小设置为无限制。默认的通常是8MB。

I'm writing a program that is to take a number between 1-10 and display all possible ways of arranging the numbers.

Ex input: 3 output:

   1 2 3
   1 3 2
   2 1 3
   2 3 1
   3 1 2
   3 2 1

Whenever I input 9 or 10, the program gives a segmentation fault and dumps the core. I believe the issue is my recursive algorithm is being called too many times. Could someone help point out how I could limit the amount of recursive calls necessary? Here is my current code:

void rearange(int numbers[11], int index, int num, int fact) {

  int temp = numbers[index];
  numbers[index] = numbers[index-1];
  numbers[index-1] = temp;

  int i;
  for (i = 1; i <= num; ++i) // print the current sequence
  {
    printf("%d ", numbers[i]);
  }
  printf("\n");

  fact--;  // decrement how many sequences remain
  index--; // decrement our index in the array

  if (index == 1) // if we're at the beginning of the array
    index = num;    // reset index to end of the array

  if (fact > 0) // If we have more sequences remaining
    rearange(numbers, index, num, fact);    // Do it all again! :D
}

int main() {
  int num, i; // our number and a counter

  printf("Enter a number less than 10: ");
  scanf("%d", &num); // get the number from the user

  int numbers[11]; // create an array of appropriate size
  // fill array
  for (i = 1; i <= num; i++) { // fill the array from 1 to num
    numbers[i] = i;
  }

  int fact = 1; // calculate the factorial to determine
  for (i = 1; i <= num; ++i) // how many possible sequences
  {
    fact = fact * i;
  }

  rearange(numbers, num, num, fact); // begin rearranging by recursion

  return 0;
}

解决方案

9! (362880) and 10! (3628800) are huge numbers that overflow the call stack when you do as many recursive calls. Because all the local variables and formal parameters have to be stored. You either you have to increase the stack size or convert the recursion into iteration.

On linux, you can do:

ulimit -s unlimited

to set the stack size to unlimited. The default is usually 8MB.

这篇关于由于递归分割故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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