递归代码问题 [英] Recursing code problem

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

问题描述

我正在使用Interactive C和我的手板(68HC11)开发系统,但

我有一个问题,我正在寻求帮助。

我是不是C的新手而是一直在学习,我只是不知道如何克服这个问题。


症状:

程序失败''运行时错误''相当于堆栈错误。

这是因为我的代码就像四处走动一样(一条蛇追逐它的

尾巴是一个这个术语我已经看过了,但是我无法理解如何克服这个问题。


我应该发布一些代码吗?在这个小组中为那些有更好的人提供了这个问题的知识来提出建议 - 我觉得这可能是一件不容易理解的事情,但我只需要一些指导

这里。


杰夫

解决方案

snowdy写道:

我正在使用Interacti用我的手板(68HC11)开发系统,但是我有一个问题,我正在寻求帮助。
我不是C的新手但是一直在学习,我只是看不到如何解决这个问题。

症状:
程序失败''运行时错误''等同于堆栈错误。
这是因为我的代码就像四处走动(一条蛇追逐它的尾巴是我曾经看过的一个术语)但是我不知道如何克服这个问题。

我是否应该在这个小组上发布一些代码,以便对这个问题有更好的了解的人提出建议 - 我觉得它可能不是很难理解,但我只需要一些指导
这里。



发布显示问题的最小的可编辑片段你有
有。


HTH,

- $


-

Artie Gold - 德克萨斯州奥斯汀


snowdy写道:


我正在使用Interactive C和我的手板(68HC11)开发系统但是
我是我遇到了一个问题,我正在寻求帮助。
我不是C的新手但是一直在学习,我只是不知道如何克服这个问题。

症状:
程序失败''运行时错误''等同于堆栈错误。
这是因为我的代码就像四处走动一样(蛇追逐它的尾巴是一个术语我已经看过了,但是我不知道如何克服这个问题。

我应该在这个小组上发布一些代码给那些有更好的人吗?知道这个问题提出建议 - 我觉得它可能不太难理解但我只需要一点指导
这里。

杰夫




发布一些代码。但是,你的症状听起来好像你没有b $ b有正确的终止条件。递归

代码的一个问题(我使用它很多,甚至写了一篇关于它的专栏)是

很容易创建一个覆盖堆栈的无限循环。


另外,一些问题并不适合递归:堆栈

变得太深而且繁荣!所以你应该分析你的代码来估算堆栈深度随着问题大小的增长而增加。


你可以找到我的专栏,Recurses!,在<

http ://www.phys.virginia.edu/classes...ogs/Cprogs.htm


-

Julian V. Noble

物理荣誉教授
jv*@lessspamformother.virginia.edu

^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/


科学只知道一条诫命:为科学做出贡献。

- Bertolt Brecht,Galileo。


" snowdy" < SN **** @ optusnet.com.au@nospam>在留言中写道

新闻:3f ********************** @ news.optusnet.com.au ...

.... snip ...

症状:
程序失败''运行时错误''等同于堆栈错误。
这是因为我的代码就像四处走动(一条蛇追逐
它的尾巴是我曾经看过的一个术语)但是我不能完全掌握如何克服这个问题。


.... snip ... Jeff




我也遇到过这个问题。这里有一些简单的尝试:

==============================
/ *

factorial.c

采用一个命令行参数:你想要的阶乘数。

休息在39左右!具有以下内容:

运行时错误R6000

- 堆栈溢出

* /


#include< stdio.h>

#include< stdlib.h>


double fact(int n);


void main(int argc,char ** argv)

{

int n;

double factorial = 1;


n = atoi(argv [1]);

fact(n);

}


双重事实(int n)

{

静态双值= 1;


if(n> 1)

value = n * fact(n-1);


printf("%3.d!=%。0f \ n" ,n,价值);


返回(价值);

}


====== ========================


所以,我有同样的问题。我如何增加筹码或以其他方式制作

这项工作以获得更高的数字?


-

ArWeGod


I am using Interactive C with my handboard (68HC11) development system but
I''ve got a problem that I am asking for help with.
I am not new to C but learning all the time and I just cant see how to
overcome this problem.

Symptom:
Programs fail '' Runtime error'' which equates to a stack error.
This is because my code is like going around and around (a snake chasing its
tail is a term I''ve seen used) but I cant quite come to grips with how to
overcome this problem.

Should I post some of the code up on this group for people with a better
knowledge of this problem to make suggestions - I feel its probably
something not difficult to understand but I just need a little guidance
here.

Jeff

解决方案

snowdy wrote:

I am using Interactive C with my handboard (68HC11) development system but
I''ve got a problem that I am asking for help with.
I am not new to C but learning all the time and I just cant see how to
overcome this problem.

Symptom:
Programs fail '' Runtime error'' which equates to a stack error.
This is because my code is like going around and around (a snake chasing its
tail is a term I''ve seen used) but I cant quite come to grips with how to
overcome this problem.

Should I post some of the code up on this group for people with a better
knowledge of this problem to make suggestions - I feel its probably
something not difficult to understand but I just need a little guidance
here.


Post the smallest compilable snippet that exhibits the problem you''re
having.

HTH,
--ag


--
Artie Gold -- Austin, Texas


snowdy wrote:


I am using Interactive C with my handboard (68HC11) development system but
I''ve got a problem that I am asking for help with.
I am not new to C but learning all the time and I just cant see how to
overcome this problem.

Symptom:
Programs fail '' Runtime error'' which equates to a stack error.
This is because my code is like going around and around (a snake chasing its
tail is a term I''ve seen used) but I cant quite come to grips with how to
overcome this problem.

Should I post some of the code up on this group for people with a better
knowledge of this problem to make suggestions - I feel its probably
something not difficult to understand but I just need a little guidance
here.

Jeff



Post some code. However, your symptoms sound like you don''t
have a proper termination condition. One problem with recursive
code (and I use it a lot and even wrote a column about it) is that
it is easy to create an endless loop that overwrites the stack.

Also, some problems are not well-suited to recursion: the stack
gets too deep and boom! So you ought to analyze your code to
estimate how the stack-depth grows with problem size.

You can find my column, Recurses!, at

http://www.phys.virginia.edu/classes...ogs/Cprogs.htm

--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".


"snowdy" <sn****@optusnet.com.au@nospam> wrote in message
news:3f**********************@news.optusnet.com.au ...
....snip...

Symptom:
Programs fail '' Runtime error'' which equates to a stack error.
This is because my code is like going around and around (a snake chasing its tail is a term I''ve seen used) but I cant quite come to grips with how to
overcome this problem.
....snip... Jeff



I''ve also run into this problem. Here''s something simple to try:
==============================
/*
factorial.c
Takes one command-line argument: the number you want the factorial of.
Breaks at around 39! with the following:
run-time error R6000
- stack overflow
*/

#include <stdio.h>
#include <stdlib.h>

double fact (int n);

void main(int argc, char **argv)
{
int n;
double factorial=1;

n = atoi (argv[1]);
fact (n);
}

double fact (int n)
{
static double value=1;

if (n > 1)
value = n * fact (n-1);

printf ("%3.d! = %.0f \n", n, value);

return (value);
}

==============================

So, I have the same question. How can I increase the stack or otherwise make
this work for higher numbers?

--
ArWeGod


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

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