[Developers] OD example on the danger of recursive programming
Jaap Glas
Jaap.Glas at dgb-group.com
Mon Oct 8 10:30:52 CEST 2007
Dear all,
As a reaction on my contribution on the danger of using recursion,
Fidel Reyes Ramos gave the following advice:
> From my point of view, I see the source of the problem described in the
> attached message is using global variables.
> Each recursion call has it own variable, so an accuarate reference is made
> in each recursive call.
> My modest advice is: When using recursion, do not use global variables, make
> them arguments of the function calling itself.
And he is right. After declaring all class member variables used in my example
next() function as arguments in the improved next( arg0, arg1 , ... ) function,
OpendTect did not crash on this anymore.
Apparently, the gcc/c++ compiler does detect tail recursion, but does not want
to take the risk in presence of global or class member variables. Since these
might be assigned to local variables within the recursive function, the result
cannot be guaranteed to be the same when performing tail call optimization in
such cases.
So my contribution was at least incomplete. I should have warned for the danger
of recursion only when the function refers to global or class member variables
inside. If one does not want to make all of them arguments of the recursive
function, the iterative solution has to be preferred.
Best regards,
Jaap Glas
--
-- dr. Jaap C. Glas
-- Software Engineer
-- dGB Earth Sciences
-- Nijverheidstraat 11-2
-- 7511 JM Enschede, The Netherlands
-- jaap.glas at dgb-group.com
-- http://www.dgb-group.com
-- Tel: +31 534315155, Fax: +31 534315104
More information about the Developers
mailing list