Avoid Recursive Functions - AMD Athlon Processor x86 Optimization Manual

X86 code optimization
Table of Contents

Advertisement

AMD Athlon™ Processor x86 Code Optimization

Avoid Recursive Functions

66
Avoid recursive functions due to the danger of overflowing the
return address stack. Convert end-recursive functions to
iterative code. An end-recursive function is when the function
call to itself is at the end of the code.
Example 1 (Avoid):
long fac(long a)
{
if (a==0) {
return (1);
} else {
return (a*fac(a–1));
}
return (t);
}
Example 2 (Preferred):
long fac(long a)
{
long t=1;
while (a > 0) {
t *= a;
a--;
}
return (t);
}
22007E/0—November 1999
Avoid Recursive Functions

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the Athlon Processor x86 and is the answer not in the manual?

Questions and answers

Table of Contents