Linking Was: Re: Proper Tail Recursive C++ (Herman Rubin)
16 Mar 1997 23:20:09 -0500

          From comp.compilers

Related articles
Proper Tail Recursive C++ (Jerry) (1997-02-20)
Re: Proper Tail Recursive C++ (William D Clinger) (1997-02-23)
Re: Proper Tail Recursive C++ (1997-03-09)
Re: Proper Tail Recursive C++ (1997-03-13)
Linking Was: Re: Proper Tail Recursive C++ (1997-03-16)
Re: Proper Tail Recursive C++ (1997-03-21)
| List of all articles for this month |

From: (Herman Rubin)
Newsgroups: comp.compilers
Date: 16 Mar 1997 23:20:09 -0500
Organization: Purdue University Statistics Department
References: 97-02-111 97-02-141 97-03-042 97-03-050
Keywords: C++, optimize

Fergus Henderson <> wrote:
> (Nathan Loofbourrow) writes:

>>William D Clinger <> writes:

>> int f (int n) {
>> int A[1000];
>> A[0] = n;
>> A[999] = n;
>> return g (A); // tail-recursive call to g
>> }

>>Well, since no one else tried it: g++ compiles this, but the result
>>crashes. I gather that this is because it assumes that A is dead
>>before the call to g(). This permits it the luxury of performing the
>>tail recursion optimization, at the expense of producing code that
>>doesn't work :-)

>I'm sure the reason that it crashes is not that g++ generates
>incorrect code, but rather that it just runs out of stack space.
>(William Clinger's code had the above function recursively invoking
>itself a million times.)

Or is the problem that the linking convention for subroutine calls
is not optimal? I have had to fight with compilers passing an
entire array to a subroutine when what should be passed is the
array address. Of course in such cases the size of the array has
to be known to the subroutine, but getting it from the new and the
old stack pointers does not seem to be optimal, either.

In the above example, it could be that the "function" g may have, as
a desired side effect, resetting A.

>I think it is a real pity that Unix systems don't bother to give a better
>error message for stack overflow than "segmentation violation".

It is a real pity that systems, and this is not restricted to Unix, do
not provide better system-user information exchange.
Herman Rubin, Dept. of Statistics, Purdue Univ., West Lafayette IN47907-1399 Phone: (765)494-6054 FAX: (765)494-0558
[This particular example was in C++, which passes arrays by reference, not
by copy unless you cook up a class that does that. -John]


Post a followup to this message

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