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

"Tim G" <timgspam@comcast.net>
8 May 2002 00:18:56 -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: "Tim G" <timgspam@comcast.net>
Newsgroups: comp.compilers
Date: 8 May 2002 00:18:56 -0400
Organization: Giganews.Com - Premium News Outsourcing
References: 02-05-024
Keywords: OOP, optimize
Posted-Date: 08 May 2002 00:18:55 EDT

"john43" wrote:
> If I have local objects that require finalization (i.e., calling some
> kind of destructor function), is it still reasonable to optimize tail
> recursion? If so, what is a good approach?
>
> For example, the destructor for object 'B' should be called at some
> point
>
> function: f( object A)
> {
> object B; // default constructor invoked
> ...
> return f( B ); // tail recursion
> }


Well, I could be wrong but I think what you want to be doing here is:


function: f (object A)
{
start:
    object B;
    ....
    A := B;
    goto start;
}


but this doesn't take into account the implicit constructor/destructor
calls, as in:


function: f (object A)
{
start:
    object B;
    // implicit constructor call
    ...
    A := B;
    // implicit destructor call 1(?)
    goto start;
    // implicit destructor call 2(?)
}


The problem seems to be that, in order to do proper tail recursion
(i.e. for this to be a proper tail recursive function), you would need
to call the destructor /before/ you make the recursive call (i.e #1).
Since, you can't destruct a parameter that you are still using, looks
like you need to make the call /after/ the recursive call (i.e. #2).
That means this isn't a proper tail-recursive function.


An alternative would be if you could remove the matching calls to the
constructor/destructor from inside your loop:


function f(object A)
{
    object B;
    // implicit constructor
start:
    ...
    A := B;
    goto start;
}


and then call the destructor just before the recursive-out of the function.
That depends a lot on your particular implementation, so I'm not sure if
it's feasable.


Just a guess. Hope this helps,
tim


Post a followup to this message

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