应该避免在C / C ++中使用递归调用吗? [英] Should one avoid using recursive call in C/C++?

查看:223
本文介绍了应该避免在C / C ++中使用递归调用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该避免在C / C ++中使用递归调用函数吗?

Should one avoid using recursive call of functions in C/C++?

我在机器学习/数据挖掘工作,所以对我来说,代码可扩展。

I work on machine learning/data mining, so it is very critical for me to make my code scalable.

当我使用Java时,我尽量避免使用递归调用,因为我经常得到我的调用堆栈溢出。虽然有选项来控制分配给调用堆栈的内存量,我认为让我的程序依赖于更少的参数是更可取的。因此,当清楚如何实现没有递归调用,可能使用由我自己管理的堆栈,我这样做。但我不确定这是一个正确的纪律,即使在Java。

When I was using Java, I avoided using recursive call as much as possible because I often got my call stack overflowed. Although there are options to control the amount of memory assigned to the call stack, I thought having my program dependent on smaller number of parameters is more desirable. Therefore when it is clear how to implement without recursive call, possibly using a stack managed by myself, I did so. But I am not sure this is a right discipline even in Java.

据我所知,在C / C ++没有调用堆栈,所以我不担心溢出它。因此,我很好奇:在程序的可扩展性方面,会尝试避免使用递归,还是鼓励,还是问题特定?

In my knowledge, there is no call stack in C/C++, so I would not worry about overflowing it. Thus, I am curious: would one try to avoid using recursion, or is it encouraged, or it is problem specific, in terms of scalability of your program?

推荐答案

C& C ++在某种程度上类似于C ++中的vtable - 就我所知,标准中没有提到,即实现不是强制的,但是为了所有实际目的,它几乎总是实现

The call stack in C & C++ is kind of like the vtable in C++ in one respect - it's not mentioned in the standard as far as I know, i.e., that implementation is not mandated, but for all practical purposes, it's almost always implemented that way.

授予一些深奥的架构,特别是在嵌入式世界中,可能会避开堆栈。 PIC是一个堆栈不友好的架构的例子,一些微小的微控制器不支持堆栈,最少不直接。在某些情况下,堆栈的使用可以消除(内联函数或积极优化函数调用,使用寄存器局部变量而不是堆栈空间)。

Granted there are some esoteric architectures, especially in the embedded world, that might eschew the stack. The PIC is an example of a stack-unfriendly architecture, and some tiny microcontrollers don't support a stack, and least not directly. And in certain situations use of the stack can be eliminated anyway (inline functions or aggressive optimization for function calls, use of registers for local variables instead of stack space).

,对你的问题...这是一个非常主观的问题,通常归结为一个问题:你能买得起吗?

Anyway, to your question... this is a very subjective question that usually boils down to one question: can you afford it?

如果你有足够的堆栈空间,并且递归不会足够深,可以吹你的堆栈,递归通常是正确的选择。不总是,但经常。

If you have enough stack space and the recursion won't be deep enough to blow your stack, recursion is often the right choice. Not always, but often.

但你必须了解你的架构,你的平台,你的工具集等,知道你的堆栈有多大。例如,对于许多多任务嵌入式系统,您的堆栈大小在创建线程/任务时确定,并且不会根据需要增长。其他简单(嵌入式)系统只使用一个堆栈,或者一个后台堆栈和一个中断堆栈。还有一些允许堆叠在限制内根据需要增长,这取决于处理器和处理器。操作系统。

But you have to understand your architecture, your platform, your toolset, etc. and know how large your stack is. With many multi-tasking embedded systems, for example, your stack size is determined at the time the thread/task is created, and it won't "grow as needed". Other simple (embedded) systems only use a single stack, or maybe one "background" stack and one interrupt stack. Still others allow the stack to grow as needed, within limits, depending on the processor & operating system.

如果你来自Java背景,如果这种讨论对你来说是新的,我不感到惊讶。

If you're coming from a Java background, I'm not surprised if this kind of discussion is new to you.

这篇关于应该避免在C / C ++中使用递归调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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