Re: when to destroy local objects w/ tail recursion ?

Joachim Durchholz <joachim_d@gmx.de>
8 May 2002 00:16:05 -0400

          From comp.compilers

Related articles
when to destroy local objects w/ tail recursion ? john43@temple.edu (john43) (2002-05-04)
Re: when to destroy local objects w/ tail recursion ? joachim_d@gmx.de (Joachim Durchholz) (2002-05-08)
Re: when to destroy local objects w/ tail recursion ? journeyman@compilerguru.com (2002-05-08)
Re: when to destroy local objects w/ tail recursion ? timgspam@comcast.net (Tim G) (2002-05-08)
| List of all articles for this month |
From: Joachim Durchholz <joachim_d@gmx.de>
Newsgroups: comp.compilers
Date: 8 May 2002 00:16:05 -0400
Organization: Compilers Central
References: 02-05-024
Keywords: OOP, optimize
Posted-Date: 08 May 2002 00:16:04 EDT

john43 wrote:
> function: f( object A)
> {
> object B; // default constructor invoked
> ...
> return f( B ); // tail recursion
> }


Note: Tail recursion is really just a special case of tail call
optimization. You can optimize the last call of every subroutine by
replacing it with a jump (modulo some caveats).


You can destruct A immediately before the tail call. Just as you can
destruct any parameters or local objects that aren't used in the
recursive calls (which is the reason why you can't destruct B).


Note that you cannot destruct A if B refers to it. This is just a
variant of the aliasing problem (aliasing is a topic for the vast
majority of optimizations). There are other caveats. Debuggers don't
particularly like tail call optimization, because data is discarded
that might interest the programmer even though it cannot affect future
computation. Control transfer across function boundaries (such as
setjmp/getjmp and exceptions) can interact unfavorably. And I dimly
recall there are other things to consider.


Regards,
Joachim


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.