递归函数中的错误处理 [英] error handlling in recursive function

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

问题描述

如何解决这个问题?假设一个递归函数内的malloc

失败,你想设置错误标志并将其返回到

调用函数(调用函数中的递归函数)

第一名)

How to to go about this ? Suppose a malloc inside a recursive function
has failed and you want to set the error flag and return it to the
calling function(the one which called the recursive function in the
first place)

推荐答案

5月30日上午9:22,pereges< Brol ... @ gmail .comwrote:
On May 30, 9:22 am, pereges <Brol...@gmail.comwrote:

如何解决这个问题?假设一个递归函数内的malloc

失败,你想设置错误标志并将其返回到

调用函数(调用函数中的递归函数)

第一名)
How to to go about this ? Suppose a malloc inside a recursive function
has failed and you want to set the error flag and return it to the
calling function(the one which called the recursive function in the
first place)



期权似乎是:


1)将旗帜传回通过递归函数堆栈,

在每次调用时检查它。这是最好的方法,如果你需要

,比如,在递归

函数的每次调用中释放资源,这似乎是可能的,因为你在
递归。


2)使用setjmp(在输入递归之前)和longjmp跳回

错误。


nb这是一个异常抛出很好的情况,因为最小的

大惊小怪它会让你回到想要处理错误的水平

清理它们之间的所有内容。但是因为我们在C,不是一个选项


-David

Options seem to be:

1) Propagate the flag back through the stack of recursive functions,
checking for it at each invocation. This is the best way if you need
to, say, release resources in each invocation of the recursive
function, which seems possible given that you are mallocing in the
recursion.

2) Use setjmp (before entering recursion) and longjmp to hop back on
error.

n.b. this is a case where exception throwing is nice, as with minimal
fuss it gets you back to the level that wants to handle the error
cleaning up all in between. But as we''re in C, not an option

-David


David Resnick< ln **** **** @ gmail.com写道:
David Resnick <ln********@gmail.comwrote:

5月30日上午9:22,pereges< Brol ... @ gmail.comwrote:
On May 30, 9:22 am, pereges <Brol...@gmail.comwrote:

如何解决这个问题?假设一个递归函数内的malloc

失败,你想设置错误标志并将其返回到

调用函数(调用函数中的递归函数)

第一名)
How to to go about this ? Suppose a malloc inside a recursive function
has failed and you want to set the error flag and return it to the
calling function(the one which called the recursive function in the
first place)


选项似乎是:
Options seem to be:


1)通过递归函数堆栈传回标志,

在每次调用时检查它。这是最好的方法,如果你需要

,比如,在递归

函数的每次调用中释放资源,这似乎是可能的,因为你在
递归。
1) Propagate the flag back through the stack of recursive functions,
checking for it at each invocation. This is the best way if you need
to, say, release resources in each invocation of the recursive
function, which seems possible given that you are mallocing in the
recursion.


2)使用setjmp(在输入递归之前)和longjmp跳回

错误。
2) Use setjmp (before entering recursion) and longjmp to hop back on
error.



第三个选项可能是拥有一个全局变量(在文件范围内)

如果发生错误则会被设置。好的,全局变量是EVIL,

但这可能是其使用可以简化的情况之一

的东西......

此致,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de


pereges写道:
pereges wrote:

>

如何解决这个问题?假设一个递归

函数内的malloc失败,你想设置错误标志并返回

它给调用函数(调用递归函数的那个​​) >
函数首先)
>
How to to go about this ? Suppose a malloc inside a recursive
function has failed and you want to set the error flag and return
it to the calling function(the one which called the recursive
function in the first place)



void * operationfails(...){

int * p;


if(p = malloc(无论如何)){

/ *做你的事情* /

}

返回p; / *如果失败则为NULL * /

}


例如。


-

[邮件]:Chuck F(cinefalconer at maineline dot net)

[page]:< http://cbfalconer.home.att.net>

尝试下载部分。

**发自 http:// www .teranews.com **

void *operationfails(...) {
int *p;

if (p = malloc(whatever)) {
/* do your thing on it */
}
return p; /* which is NULL for failure */
}

for example.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **


这篇关于递归函数中的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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