Sponsored Content
Full Discussion: Recursion
Top Forums Programming Recursion Post 302263676 by otheus on Tuesday 2nd of December 2008 08:09:53 AM
Old 12-02-2008
You've been around long enough not to post homework questions, so I'll answer: use setjmp() and longjmp(). Basically, the first call saves the current stack configuration (stack pointer, parent caller address, etc). The second call restores that stack configuration. So you might have some code like this:
Code:
#include <setjmp.h>
int found = 0;
jmp_buf origin; 
main() {
  setjmp(&origin);
  if (found == 0) 
     recursive_call();
}

recursive_call() {

   /* HERE: set found to 1 at some point */
   /* then... */
   if (found == 0) 
      recursive_call();
   else 
      longjmp( &origin );
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

recursion

I'm using the UNIX csh and i wish to use recursion to nav my way up (or down as it is) a given folder. My little test script is called "r" and takes a folder as argv (or $1) #!/bin/tcsh -f set allFiles = `ls -A $argv` cd $argv while ($#allFiles) if (-d... (1 Reply)
Discussion started by: gsjf
1 Replies

2. Shell Programming and Scripting

recursion too deep

I am running a korn shell script which has a recursive function. The script ran for 117 iterations and ended up with the following error "recursion too deep". what should be done to avert this? Thanks in advance Swamy p.s. I am on UNIX MPRAS V4 (3 Replies)
Discussion started by: swamy455
3 Replies

3. Shell Programming and Scripting

Help Help Help in recursion

Hello every body. I am trying to find the factorial using the following code. But it is giving the syntax error. I tried very much but in vain. Thanks in advance for helping me factorial() { if then y=`expr $1 - 1` x=$(( $1 \* factorial $y ))... (6 Replies)
Discussion started by: murtaza
6 Replies

4. Shell Programming and Scripting

recursion script problem

Hi Guys,, I tried to create a recursive function in unix. The following is the code. #/bin/sh function(){ n=$1; if ; then out=1; echo "inside if for 0"; else out = `$n * function "$n-1"`; echo "inside if for $n-1; fi (3 Replies)
Discussion started by: mac4rfree
3 Replies

5. Programming

C Recursion (explain)

Hi, Question: how come the output is like that? Can explain to me abit. I am learning C. Thanks! #include <stdio.h> #include <string.h> void printit(char line_of_char, int index); int main() { char line_of_char; int index = -1; strcpy(line_of_char, "This is a string."); ... (5 Replies)
Discussion started by: seede
5 Replies

6. Shell Programming and Scripting

script recursion

Can someone please explain me why the following script calls it self recursively: #!/bin/bash echo Called $0 while this not: #!/bin/bash echo Called $($0) Thanks (6 Replies)
Discussion started by: superpointer
6 Replies

7. UNIX for Advanced & Expert Users

Recursion list for rm -R in find

In the following command: find / -ctime +3 -exec rm -R {}\; how is the recursion list built for the actual rm ? F'rinstance; I had a case where a user typed this as root using '/' instead of '.' so everything in the root level was going to be traversed. They hit <ctrl>C before too much was... (5 Replies)
Discussion started by: port43
5 Replies

8. Shell Programming and Scripting

Bash variable recursion

Not sure how to ask this question. I want concatenate strings and variable recursively into new variable. For example: infile01=/dir/subfolder/file01.txt infile02=/dir/subfolder/file02.txt infile03=/dir/subfolder/file03.txt for i in {01..03} do u=${"infile"$i} echo $u doneI got error... (7 Replies)
Discussion started by: yifangt
7 Replies

9. Solaris

BIND 9, disable recursion

Hi, I am trying to disable the recursion on DNS server (Solaris 10). I have added the lines in the named.conf as below: allow-query-cache { none; }; recursion no; Then restarted the solaris DNS services svcadm refresh svc:/network/dns/server:default Still I am able to... (0 Replies)
Discussion started by: snchaudhari2
0 Replies

10. UNIX for Beginners Questions & Answers

Copy directory withOUT recursion

Hi, I cannot find a way to copy a directory to another location with all attributes (mode, ownership, timestamps) but withOUT recursion (after so many years of working with Linux). Say I want to create /home/jail/tmp exactly like /tmp but with nothing in it. Here is what I tried: ... (7 Replies)
Discussion started by: chebarbudo
7 Replies
PCRESTACK(3)						     Library Functions Manual						      PCRESTACK(3)

NAME
PCRE - Perl-compatible regular expressions PCRE DISCUSSION OF STACK USAGE
When you call pcre_exec(), it makes use of an internal function called match(). This calls itself recursively at branch points in the pat- tern, in order to remember the state of the match so that it can back up and try a different alternative if the first one fails. As match- ing proceeds deeper and deeper into the tree of possibilities, the recursion depth increases. Not all calls of match() increase the recursion depth; for an item such as a* it may be called several times at the same level, after matching different numbers of a's. Furthermore, in a number of cases where the result of the recursive call would immediately be passed back as the result of the current call (a "tail recursion"), the function is just restarted instead. The pcre_dfa_exec() function operates in an entirely different way, and hardly uses recursion at all. The limit on its complexity is the amount of workspace it is given. The comments that follow do NOT apply to pcre_dfa_exec(); they are relevant only for pcre_exec(). You can set limits on the number of times that match() is called, both in total and recursively. If the limit is exceeded, an error occurs. For details, see the section on extra data for pcre_exec() in the pcreapi documentation. Each time that match() is actually called recursively, it uses memory from the process stack. For certain kinds of pattern and data, very large amounts of stack may be needed, despite the recognition of "tail recursion". You can often reduce the amount of recursion, and therefore the amount of stack used, by modifying the pattern that is being matched. Consider, for example, this pattern: ([^<]|<(?!inet))+ It matches from wherever it starts until it encounters "<inet" or the end of the data, and is the kind of pattern that might be used when processing an XML file. Each iteration of the outer parentheses matches either one character that is not "<" or a "<" that is not followed by "inet". However, each time a parenthesis is processed, a recursion occurs, so this formulation uses a stack frame for each matched char- acter. For a long string, a lot of stack is required. Consider now this rewritten pattern, which matches exactly the same strings: ([^<]++|<(?!inet))+ This uses very much less stack, because runs of characters that do not contain "<" are "swallowed" in one item inside the parentheses. Recursion happens only when a "<" character that is not followed by "inet" is encountered (and we assume this is relatively rare). A pos- sessive quantifier is used to stop any backtracking into the runs of non-"<" characters, but that is not related to stack usage. This example shows that one way of avoiding stack problems when matching long subject strings is to write repeated parenthesized subpat- terns to match more than one character whenever possible. Compiling PCRE to use heap instead of stack In environments where stack memory is constrained, you might want to compile PCRE to use heap memory instead of stack for remembering back- up points. This makes it run a lot more slowly, however. Details of how to do this are given in the pcrebuild documentation. When built in this way, instead of using the stack, PCRE obtains and frees memory by calling the functions that are pointed to by the pcre_stack_malloc and pcre_stack_free variables. By default, these point to malloc() and free(), but you can replace the pointers to cause PCRE to use your own functions. Since the block sizes are always the same, and are always freed in reverse order, it may be possible to implement customized memory handlers that are more efficient than the standard functions. Limiting PCRE's stack usage PCRE has an internal counter that can be used to limit the depth of recursion, and thus cause pcre_exec() to give an error code before it runs out of stack. By default, the limit is very large, and unlikely ever to operate. It can be changed when PCRE is built, and it can also be set when pcre_exec() is called. For details of these interfaces, see the pcrebuild and pcreapi documentation. As a very rough rule of thumb, you should reckon on about 500 bytes per recursion. Thus, if you want to limit your stack usage to 8Mb, you should set the limit at 16000 recursions. A 64Mb stack, on the other hand, can support around 128000 recursions. The pcretest test program has a command line option (-S) that can be used to increase the size of its stack. Changing stack size in Unix-like systems In Unix-like environments, there is not often a problem with the stack unless very long strings are involved, though the default limit on stack size varies from system to system. Values from 8Mb to 64Mb are common. You can find your default limit by running the command: ulimit -s Unfortunately, the effect of running out of stack is often SIGSEGV, though sometimes a more explicit error message is given. You can nor- mally increase the limit on stack size by code such as this: struct rlimit rlim; getrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = 100*1024*1024; setrlimit(RLIMIT_STACK, &rlim); This reads the current limits (soft and hard) using getrlimit(), then attempts to increase the soft limit to 100Mb using setrlimit(). You must do this before calling pcre_exec(). Changing stack size in Mac OS X Using setrlimit(), as described above, should also work on Mac OS X. It is also possible to set a stack size when linking a program. There is a discussion about stack sizes in Mac OS X at this web site: http://developer.apple.com/qa/qa2005/qa1419.html. AUTHOR
Philip Hazel University Computing Service Cambridge CB2 3QH, England. REVISION
Last updated: 09 July 2008 Copyright (c) 1997-2008 University of Cambridge. PCRESTACK(3)
All times are GMT -4. The time now is 04:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy