If you have a nice recursive algorithm but fear of overrunning stack, you can easily keep track of your recursion depth with an additional parameter you decrement each call and compare to a (hopefully reasonably choosen) maximum recursion depth (exiting with an error when the threshold has been reached).
This obviously uses extra stack space (and a little performance), so its up to you to to decide if you want to keep it in production code or remove it and call your testing (and additional safety marging due to its removal) good enough to be safe.

Avoiding recursion results in having an acyclic function call graph, which code analyzers can exploit to prove limits on stack use and boundedness of exe-cutions.
There is another topic about following or not the programming guidelines.
Quoting from the attached PDF: https://www.eevblog.com/forum/chat/dogmatic-coding-standards/msg2636658/#msg2636658QuoteAvoiding recursion results in having an acyclic function call graph, which code analyzers can exploit to prove limits on stack use and boundedness of exe-cutions.
... I've been wondering about the "control" on stack usage in recursion programming, seeing people raised lots of concerns.
My simple question is, if running a recursive program, without any control or aware of the stack limitation, isn't that like using a cpu by designed, that can't handle say like div by zero (eg. NMI or etc) from the 1st place ?
If you have a nice recursive algorithm but fear of overrunning stack, you can easily keep track of your recursion depth with an additional parameter you decrement each call and compare to a (hopefully reasonably choosen) maximum recursion depth (exiting with an error when the threshold has been reached).
This obviously uses extra stack space (and a little performance), so its up to you to to decide if you want to keep it in production code or remove it and call your testing (and additional safety marging due to its removal) good enough to be safe.

If you have a nice recursive algorithm but fear of overrunning stack, you can easily keep track of your recursion depth with an additional parameter you decrement each call and compare to a (hopefully reasonably choosen) maximum recursion depth (exiting with an error when the threshold has been reached).
This obviously uses extra stack space (and a little performance), so its up to you to to decide if you want to keep it in production code or remove it and call your testing (and additional safety marging due to its removal) good enough to be safe.
Not an expert in programming, nor experienced.
I've been wondering about the "control" on stack usage in recursion programming, seeing people raised lots of concerns.
My simple question is, if running a recursive program, without any control or aware of the stack limitation, isn't that like using a cpu by designed, that can't handle say like div by zero (eg. NMI or etc) from the 1st place ?
Edit : Simpler analogy, driving a tank with eyes closed, that you can't really be sure that you're crushing your enemy (expected & wanted) or your friends.
Cmiiw.
In most cases it is very difficult to work out the worst case biggest possible stack requirement, and make sure you have allowed enough stack space for that.
In most cases it is very difficult to work out the worst case biggest possible stack requirement, and make sure you have allowed enough stack space for that.
I don't think so. At any rate, it's much harder to predict heap behaviour, which, unlike stack, can be fragmented, possibly after days or months of running.
In most cases it is very difficult to work out the worst case biggest possible stack requirement, and make sure you have allowed enough stack space for that.
I don't think so. At any rate, it's much harder to predict heap behaviour, which, unlike stack, can be fragmented, possibly after days or months of running.
Feynman worked out in some detail the program for
computing Hopfield's network on the Connection Ma-
chine. The part that he was proudest of was the
subroutine for computing a logarithm. I mention it here
not only because it is a clever algorithm, but also
because it is a specific contribution Richard made to the
mainstream of computer science. He had invented it at
Los Alamos.
Consider the problem of finding the logarithm of a
fractional number between 1 and 2. (The algorithm can be
generalized without too much difficulty.) Feynman ob-
served that any such number can be uniquely represented
as a product of numbers of the form 1 + 2 -k, where k is an
integer. Testing for the presence of each of these factors in
a binary representation is simply a matter of a shift and a
subtraction. Once the factors are determined, the loga-
rithm can be computed by adding together the precomput-
ed logarithms of the factors. The algorithm fit the
Connection Machine especially well because the small
table of the logarithms of 1 + 2 -k could be shared by all
the processors. The entire computation took less time
than doing a division.
Quarter square multiplication, one of the shift-and-add algorithms: Let's say you know Sn = floor(n2/4), i.e. Sn is a quarter of n squared, rounded towards zero. If x ≥ y ≥ 0, then x·y = Sx+y - Sx-y.
I wonder if this could be used to implement a fast multiplication on an FPGA? I dunno.
for larger ones, I think the size of the required look-up table would quickly get impractical. Ideas?
Quarter square multiplication, one of the shift-and-add algorithms: Let's say you know Sn = floor(n2/4), i.e. Sn is a quarter of n squared, rounded towards zero. If x ≥ y ≥ 0, then x·y = Sx+y - Sx-y.
I wonder if this could be used to implement a fast multiplication on an FPGA? I dunno.
how much code complexity does the recursion save you?
In most cases it is very difficult to work out the worst case biggest possible stack requirement, and make sure you have allowed enough stack space for that.
On bigger machines most people do little more than allow lots of stack space and hope for the best. On small embedded machines you have to get your stack allocation right, as there is little RAM to waste. You might get some help from static analysis tools, but those tools are not generally foolproof. For example, they don't usually have the ability to work out the worst case program call depth + the worst case mix of nested interrupts.
We're talking about B-trees here. With disk-based blocks. What's your block size?
btree_key_t key[DEFAULT_ORDER];
btree_iblock_t iblock[DEFAULT_ORDER];
In most cases it is very difficult to work out the worst case biggest possible stack requirement
Quarter square multiplication, one of the shift-and-add algorithms: Let's say you know Sn = floor(n2/4), i.e. Sn is a quarter of n squared, rounded towards zero. If x ≥ y ≥ 0, then x·y = Sx+y - Sx-y.
I wonder if this could be used to implement a fast multiplication on an FPGA? I dunno.The critical importance of fast, silicon efficient, multiplication for DSP applications means a huge number of avenues to faster multiplication have been tried.
Again, I have zero experience with FPGAs, so I do not know.
Again, I have zero experience with FPGAs, so I do not know.FPGAs usually have dedicated DSP units on silicon and vendors usually do not tell you about implementation details.
about multiplications