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) 10 1724
On May 30, 9:22 am, pereges <Brol...@gmail.comwrote:
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) 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.comwrote:
On May 30, 9:22 am, pereges <Brol...@gmail.comwrote:
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) 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.
A third option might be to have a global variable (at file scope)
that gets set if an error occurs. Ok, global variables are EVIL,
but this may be one of the cases where their use can simplify
things a bit...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
pereges wrote:
>
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(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 **
On May 30, 11:46 am, j...@toerring.de (Jens Thoms Toerring) wrote:
David Resnick <lndresn...@gmail.comwrote:
On May 30, 9:22 am, pereges <Brol...@gmail.comwrote:
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) 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.
A third option might be to have a global variable (at file scope)
that gets set if an error occurs. Ok, global variables are EVIL,
but this may be one of the cases where their use can simplify
things a bit...
Sure, and a fourth is to pass down a pointer to a variable to use for
error reporting. I gave the answer I did because I interpreted his
question as being also how to reasonably unwind the stack of recursive
invocations when hitting an error condition...
-David
On May 30, 10:17*am, David Resnick <lndresn...@gmail.comwrote:
On May 30, 11:46 am, j...@toerring.de (Jens Thoms Toerring) wrote:
David Resnick <lndresn...@gmail.comwrote:
On May 30, 9:22 am, pereges <Brol...@gmail.comwrote:
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) 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.
A third option might be to have a global variable (at file scope)
that gets set if an error occurs. Ok, global variables are EVIL,
but this may be one of the cases where their use can simplify
things a bit...
Sure, and a fourth is to pass down a pointer to a variable to use for
error reporting. *I gave the answer I did because I interpreted his
question as being also how to reasonably unwind the stack of recursive
invocations when hitting an error condition...
Yet another one is to use signal()/raise(). E.g.: http://publications.gbdirect.co.uk/c..._handling.html
In article <19**********************************@x35g2000hsb. googlegroups.com>,
user923005 <dc*****@connx.comwrote:
>On May 30, 10:17=A0am, David Resnick <lndresn...@gmail.comwrote:
>Sure, and a fourth is to pass down a pointer to a variable to use for error reporting.
>Yet another one is to use signal()/raise().
If the routine so invoked does not terminate with longjump
(and longjump was already proffered earlier in the list) then
when the routine returns, execution will resume with the return
of raise() (which will have a value of 0 if successful, non-zero
otherwise.)
signal()/raise() does have the advantage that the invoked routine
is able to access library functions, and is able to access static storage
that is not volatile sig_atomic_t (undefined behaviour if the
invocation of the signal'd routine does not come from raise()).
Effectively, signal()/raise() becomes a method for storing a hidden
global pointer to a subroutine that gets called when raise() is used...
nothing you couldn't easily duplicate. Hmmm, I bet there has already
been an IOCC entry (or five) that relied upon this...
--
"I want to be remembered as the guy who gave his all whenever
he was on the field." -- Walter Payton
On 30 May 2008 at 19:27, Walter Roberson wrote:
If the routine so invoked does not terminate with longjump
What's longjump?
(Think I'll make it as a pedant? :) )
In article <sl*******************@nospam.invalid>,
Antoninus Twink <no****@nospam.invalidwrote:
>On 30 May 2008 at 19:27, Walter Roberson wrote:
>If the routine so invoked does not terminate with longjump
>What's longjump?
'jmp' was considered to be a machine-specific instruction, so in
TC9 longjmp will be deprecated and the more general longjump substituted
instead. ;-)
>(Think I'll make it as a pedant? :) )
Always hoped that I'd be a pedant
Knew that I would make it if I tried (If I tried)
Then when we retire we can write the corrigenda
So they'll still talk about us when we've died
(With apologies to Mr. Webber)
--
"It's a hard life sometimes and the biggest temptation is to let
how hard it is be an excuse to weaken." -- Walter Dean Myers
"David Resnick" <ln********@gmail.comwrote in message
>A third option might be to have a global variable (at file scope) that gets set if an error occurs. Ok, global variables are EVIL, but this may be one of the cases where their use can simplify things a bit...
Sure, and a fourth is to pass down a pointer to a variable to use for
error reporting. I gave the answer I did because I interpreted his
question as being also how to reasonably unwind the stack of recursive
invocations when hitting an error condition...
MiniBasic has exactly this problem.
The solution is to have a sticky error. So once one error is set, all
subsequent errors are suppressed. So the rest of the code can chug on,
returning in its own good time - obviously you have to be a bit careful not
to write to potentially null pointers and so on, but there is no reason to
stop expression parsing jsut because a string wasn't allocated somewhere.
PS website contains important programming and scientific information.
Someone please check availability for me.
--
Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm
On May 30, 12:27 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
In article <19b77d08-9400-4bcc-b5ef-112545eda...@x35g2000hsb.googlegroups.com>,
user923005 <dcor...@connx.comwrote:
On May 30, 10:17=A0am, David Resnick <lndresn...@gmail.comwrote:
Sure, and a fourth is to pass down a pointer to a variable to use for
error reporting.
Yet another one is to use signal()/raise().
If the routine so invoked does not terminate with longjump
(and longjump was already proffered earlier in the list) then
when the routine returns, execution will resume with the return
of raise() (which will have a value of 0 if successful, non-zero
otherwise.)
signal()/raise() does have the advantage that the invoked routine
is able to access library functions,
I don't get what you mean when you say "signal()/raise() does have the
advantage that the invoked routine is able to access library
functions, "
Can you give an example of this?
Chad This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: tevans |
last post by:
I'm using Slackware GNU/Linux 9.0 on an Intel PII 266 machine that's SMP
capable, but only with one CPU installed, and 64MB of RAM. It's
basically a small test system that I play around with in...
|
by: |
last post by:
OK:
Purpose: Using user's input and 3 recursive functions, construct an hour
glass figure. Main can only have user input, loops and function calls.
Recursive function 1 takes input and displays...
|
by: Victor |
last post by:
Hello,
I've got a situation in which the number of (valid) recursive calls I
make will cause stack overflow. I can use getrlimit (and setrlimit)
to test (and set) my current stack size. ...
|
by: Red |
last post by:
I am taking a c++ course. I have a simple program that just wont compile and
I cant seem to figure out why. If I compile the class file without
referencing it in the int main() it will compile but...
|
by: Csaba Gabor |
last post by:
Inside a function, I'd like to know the call stack. By this I mean
that I'd like to know the function that called this one, that one's
caller and so on.
So I thought to do:
<script...
|
by: etienne |
last post by:
Hello,
I'm trying to compile this C++ code with gcc 3.4.5 under Solaris/SPARC
(from a complete application, the file is : FullLinkedList.hh) but the
make fails (no problem when compiling under...
|
by: erik.erikson |
last post by:
I am getting a compiler error that I can't well explain or even
understand the origin of (though I boiled it down close...). Below is
a bare-bones example.
What I am doing is defining the...
|
by: broli |
last post by:
Is it generally a good idea to use perror() function to handle all the
error situations ?
For eg in one of my modules I have used it extensively.
int reader()
{
FILE *zeus_file;...
|
by: from.future.import |
last post by:
Hi,
I encountered garbage collection behaviour that I didn't expect when
using a recursive function inside another function: the definition of
the inner function seems to contain a circular...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |