I understand that for C at least the stack frame and return address are written to the stack every time the recursive function is called, but is there an obscure way of making it not run out of memory? Obviously this is purely a hypothetical question as I can't imagine a use case for it.
Can infinite recursion be implemented?
378 Views Asked by Boba0514 At
1
There are 1 best solutions below
Related Questions in RECURSION
- Needing a private and public method for the same recursive function
- Recursive function in PHP function : how to prevent return value?
- Json implicit format with recursive class definition
- java update all children in list
- recursively editing member variable: All instances have same value
- Editing pseudo_encrypt PostgreSQL function with Recurrsion to Avoid Certain IDs
- How to copy elements from array one to array two using recursion?
- create_progress_bar in recursive functions in R
- How to check that all values are equal in array using recursion?
- String List of filepaths to structured object
- FIFO Stock Valuation Through CTE-Recursion
- Recursion - nth element from last in a linkedlist
- Parsing user entered int into digits not working C++
- reverse a linked list using recursion error
- How to find Relationships between Objects
Related Questions in MEMORY
- DataTable does not release memory
- Impala Resource Estimation for queries with Group by
- Is there any way to get a lru list in Linux kernel?
- C# console application - Unhandled exception while finding the Available and free Ram space.Getting exact answer in windows forms application
- Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in PHP
- C# equivalent of Java Memory mapping methods
- How to figure out the optimal fetch size for the select query
- Creating two arrays with malloc on the same line
- Using parse.com and having allocation memory issue
- error reading variable: cannot access memory at address
- CentOS memory availability
- Correct idiom for freeing repr(C) structs using Drop trait
- Find Ram/Memory manufacturer in Linux?
- Profiling memory usage on App Engine
- Access Violation: 0xC0000005, why is this happening?
Related Questions in STACK
- SQL FIFO STACK using two tables
- C++ assign const reference to instance variable (memory issues?)
- Efficient retrieval of last and second to last element of ArrayStack in Scala?
- How to implement Exception for function isFull() on Stack
- Merging two sorted stacks
- MASM console window creation troubles (maybe my stack frame??)
- Why does the Java.Util.Stack not pop last element in the loop?
- popStack pops out data that I didn't push (stack adt)
- kill function from ISR on cortex-m0
- Why in C++ overwritingis is slower than writing?
- why does a integer type need to be little-endian?
- fread(), solaris to unix portability and use of uninitialised values
- How can I have different color for each bar of stack barplots? in R
- Stack with push and pop functions not working
- c++ memory allocated at compile time
Related Questions in STACK-FRAME
- Visual Studio loses first stack frames on StackOverflowException
- How to move up and down between stack frames?
- What is the difference between stack frame and execution frame?
- Using x86's ENTER instruction with a non-zero nesting level?
- How to disable RBP frame pointer register optimization in GCC when using -O*?
- Java Heap and Stack
- (x86) Is the value of ESP relative to EBP, or not?
- gdb corefile not see function parameters
- call by value/call by reference assembly
- Does -fomit-frame-pointer *always* omit the fp?
- repr the call resulting in a stack frame?
- Is the gcc insane optimisation level (-O3) not insane enough?
- Is there a simple way to obtain all the local variables in the current stack frame in C# (or CIL)
- StackFrame.GetFileLineNumber() behaviour varies based on assembly Platform and Optimisation flags
- Java performance testing changes when executing code from callback class. Java stack frame issue?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You can emulate recursion using a stack
The part of memory related to function calls and static variables (declared with
int x;in C) is separate from the part of memory related to dynamic allocation (usingmalloc()in C). Only the former, called "the stack" is limited and will lead to a "Stack Overflow" error. Well, of course that's not entirely true. The latter is called "the heap" and of course your computer is not magic and will run out of memory at some point if you really try to push its limits.You can use tail-recursion to avoid adding layers to the call stack
Stack overflow is due to the size of the call stack. Imagine a function like this:
When making the recursive call to
f, the computer needs to keep some note of the fact that we'll need to do one more multiplication once the recursive call is completed. Taking note of this is achieved by adding a layer to a "call stack" with some information on the values of variables, and where in the code we are. This requires memory and will lead to stack overflow in case the stack becomes too big.Now compare with the following code:
This time the recursive call is directly encapsulated in the
return, meaning there is no more work to do after the recursive call. Imagine you asked me to do a job and report the result to you; by making the recursive call I'm delegating some work to my friend; then instead of staying around waiting for my friend to report back to me so that I can report back to you, I leave immediately and tell my friend to report directly to you. This saves memory by "cutting the middle man".Read more:
In languages that feature lazy evaluation, you can write a seemingly infinitely-recursive function, then only evaluate it as far as required: