<s>
In	O
software	O
,	O
a	O
stack	B-Error_Name
overflow	I-Error_Name
occurs	O
if	O
the	O
call	B-General_Concept
stack	I-General_Concept
pointer	O
exceeds	O
the	O
stack	B-Application
bound	O
.	O
</s>
<s>
The	O
call	B-General_Concept
stack	I-General_Concept
may	O
consist	O
of	O
a	O
limited	O
amount	O
of	O
address	B-General_Concept
space	I-General_Concept
,	O
often	O
determined	O
at	O
the	O
start	O
of	O
the	O
program	O
.	O
</s>
<s>
The	O
size	O
of	O
the	O
call	B-General_Concept
stack	I-General_Concept
depends	O
on	O
many	O
factors	O
,	O
including	O
the	O
programming	O
language	O
,	O
machine	O
architecture	O
,	O
multi-threading	O
,	O
and	O
amount	O
of	O
available	O
memory	O
.	O
</s>
<s>
When	O
a	O
program	O
attempts	O
to	O
use	O
more	O
space	O
than	O
is	O
available	O
on	O
the	O
call	B-General_Concept
stack	I-General_Concept
(	O
that	O
is	O
,	O
when	O
it	O
attempts	O
to	O
access	O
memory	O
beyond	O
the	O
call	B-General_Concept
stack	I-General_Concept
's	O
bounds	O
,	O
which	O
is	O
essentially	O
a	O
buffer	B-General_Concept
overflow	I-General_Concept
)	O
,	O
the	O
stack	B-Application
is	O
said	O
to	O
overflow	O
,	O
typically	O
resulting	O
in	O
a	O
program	B-General_Concept
crash	I-General_Concept
.	O
</s>
<s>
The	O
most-common	O
cause	O
of	O
stack	B-Error_Name
overflow	I-Error_Name
is	O
excessively	O
deep	O
or	O
infinite	O
recursion	O
,	O
in	O
which	O
a	O
function	O
calls	O
itself	O
so	O
many	O
times	O
that	O
the	O
space	O
needed	O
to	O
store	O
the	O
variables	O
and	O
information	O
associated	O
with	O
each	O
call	O
is	O
more	O
than	O
can	O
fit	O
on	O
the	O
stack	B-Application
.	O
</s>
<s>
An	O
example	O
of	O
infinite	O
recursion	O
in	O
C	B-Language
.	O
</s>
<s>
The	O
function	O
foo	O
,	O
when	O
it	O
is	O
invoked	O
,	O
continues	O
to	O
invoke	O
itself	O
,	O
allocating	O
additional	O
space	O
on	O
the	O
stack	B-Application
each	O
time	O
,	O
until	O
the	O
stack	B-Error_Name
overflows	I-Error_Name
resulting	O
in	O
a	O
segmentation	B-Error_Name
fault	I-Error_Name
.	O
</s>
<s>
However	O
,	O
some	O
compilers	O
implement	O
tail-call	B-Language
optimization	I-Language
,	O
allowing	O
infinite	O
recursion	O
of	O
a	O
specific	O
sort	O
—	O
tail	B-Language
recursion	I-Language
—	O
to	O
occur	O
without	O
stack	B-Error_Name
overflow	I-Error_Name
.	O
</s>
<s>
This	O
works	O
because	O
tail-recursion	B-Language
calls	O
do	O
not	O
take	O
up	O
additional	O
stack	B-Application
space	O
.	O
</s>
<s>
Some	O
C	B-Language
compiler	O
options	O
will	O
effectively	O
enable	O
tail-call	B-Language
optimization	I-Language
;	O
for	O
example	O
,	O
compiling	O
the	O
above	O
simple	O
program	O
using	O
gcc	B-Application
with	O
-O1	O
will	O
result	O
in	O
a	O
segmentation	B-Error_Name
fault	I-Error_Name
,	O
but	O
not	O
when	O
using	O
-O2	O
or	O
-O3	O
,	O
since	O
these	O
optimization	O
levels	O
imply	O
the	O
-foptimize-sibling-calls	O
compiler	O
option	O
.	O
</s>
<s>
Other	O
languages	O
,	O
such	O
as	O
Scheme	B-Language
,	O
require	O
all	O
implementations	O
to	O
include	O
tail-recursion	B-Language
as	O
part	O
of	O
the	O
language	O
standard	O
.	O
</s>
<s>
A	O
recursive	O
function	O
that	O
terminates	O
in	O
theory	O
but	O
causes	O
a	O
call	B-General_Concept
stack	I-General_Concept
buffer	B-General_Concept
overflow	I-General_Concept
in	O
practice	O
can	O
be	O
fixed	O
by	O
transforming	O
the	O
recursion	O
into	O
a	O
loop	O
and	O
storing	O
the	O
function	O
arguments	O
in	O
an	O
explicit	O
stack	B-Application
(	O
rather	O
than	O
the	O
implicit	O
use	O
of	O
the	O
call	B-General_Concept
stack	I-General_Concept
)	O
.	O
</s>
<s>
This	O
is	O
always	O
possible	O
because	O
the	O
class	O
of	O
primitive	B-Architecture
recursive	I-Architecture
functions	I-Architecture
is	O
equivalent	O
to	O
the	O
class	O
of	O
LOOP	O
computable	O
functions	O
.	O
</s>
<s>
Consider	O
this	O
example	O
in	O
C++	B-Language
-like	O
pseudocode	O
:	O
</s>
<s>
}	O
stack.push(argument )	O
;	O
</s>
<s>
argument	O
=	O
stack.pop( )	O
;	O
</s>
<s>
stack.push(argument.next )	O
;	O
</s>
<s>
A	O
primitive	B-Architecture
recursive	I-Architecture
function	I-Architecture
like	O
the	O
one	O
on	O
the	O
left	O
side	O
can	O
always	O
be	O
transformed	O
into	O
a	O
loop	O
like	O
on	O
the	O
right	O
side	O
.	O
</s>
<s>
A	O
function	O
like	O
the	O
example	O
above	O
on	O
the	O
left	O
would	O
not	O
be	O
a	O
problem	O
in	O
an	O
environment	O
supporting	O
tail-call	B-Language
optimization	I-Language
;	O
however	O
,	O
it	O
is	O
still	O
possible	O
to	O
create	O
a	O
recursive	O
function	O
that	O
may	O
result	O
in	O
a	O
stack	B-Error_Name
overflow	I-Error_Name
in	O
these	O
languages	O
.	O
</s>
<s>
Both	O
pow(base, exp )	O
functions	O
above	O
compute	O
an	O
equivalent	O
result	O
,	O
however	O
,	O
the	O
one	O
on	O
the	O
left	O
is	O
prone	O
to	O
causing	O
a	O
stack	B-Error_Name
overflow	I-Error_Name
because	O
tail-call	B-Language
optimization	I-Language
is	O
not	O
possible	O
for	O
this	O
function	O
.	O
</s>
<s>
During	O
execution	O
,	O
the	O
stack	B-Application
for	O
these	O
functions	O
will	O
look	O
like	O
this	O
:	O
</s>
<s>
Notice	O
that	O
the	O
function	O
on	O
the	O
left	O
must	O
store	O
in	O
its	O
stack	B-Application
exp	O
number	O
of	O
integers	O
,	O
which	O
will	O
be	O
multiplied	O
when	O
the	O
recursion	O
terminates	O
and	O
the	O
function	O
returns	O
1	O
.	O
</s>
<s>
As	O
no	O
other	O
information	O
outside	O
of	O
the	O
current	O
function	O
invocation	O
must	O
be	O
stored	O
,	O
a	O
tail-recursion	B-Language
optimizer	O
can	O
"	O
drop	O
"	O
the	O
prior	O
stack	B-Application
frames	O
,	O
eliminating	O
the	O
possibility	O
of	O
a	O
stack	B-Error_Name
overflow	I-Error_Name
.	O
</s>
<s>
The	O
other	O
major	O
cause	O
of	O
a	O
stack	B-Error_Name
overflow	I-Error_Name
results	O
from	O
an	O
attempt	O
to	O
allocate	O
more	O
memory	O
on	O
the	O
stack	B-Application
than	O
will	O
fit	O
,	O
for	O
example	O
by	O
creating	O
local	O
array	O
variables	O
that	O
are	O
too	O
large	O
.	O
</s>
<s>
For	O
this	O
reason	O
some	O
authors	O
recommend	O
that	O
arrays	O
larger	O
than	O
a	O
few	O
kilobytes	O
should	O
be	O
allocated	B-Language
dynamically	I-Language
instead	O
of	O
as	O
a	O
local	O
variable	O
.	O
</s>
<s>
An	O
example	O
of	O
a	O
very	O
large	O
stack	B-Application
variable	O
in	O
C	B-Language
:	O
</s>
<s>
On	O
a	O
C	B-Language
implementation	O
with	O
8	O
byte	O
double-precision	O
floats	O
,	O
the	O
declared	O
array	O
consumes	O
8	O
megabytes	O
of	O
data	O
;	O
if	O
this	O
is	O
more	O
memory	O
than	O
is	O
available	O
on	O
the	O
stack	B-Application
(	O
as	O
set	O
by	O
thread	O
creation	O
parameters	O
or	O
operating	O
system	O
limits	O
)	O
,	O
a	O
stack	B-Error_Name
overflow	I-Error_Name
will	O
occur	O
.	O
</s>
<s>
Stack	B-Error_Name
overflows	I-Error_Name
are	O
made	O
worse	O
by	O
anything	O
that	O
reduces	O
the	O
effective	O
stack	B-Application
size	O
of	O
a	O
given	O
program	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
same	O
program	O
being	O
run	O
without	O
multiple	O
threads	O
might	O
work	O
fine	O
,	O
but	O
as	O
soon	O
as	O
multi-threading	O
is	O
enabled	O
the	O
program	O
will	O
crash	B-General_Concept
.	O
</s>
<s>
This	O
is	O
because	O
most	O
programs	O
with	O
threads	O
have	O
less	O
stack	B-Application
space	O
per	O
thread	O
than	O
a	O
program	O
with	O
no	O
threading	O
support	O
.	O
</s>
<s>
Because	O
kernels	B-Operating_System
are	O
generally	O
multi-threaded	O
,	O
people	O
new	O
to	O
kernel	B-Operating_System
development	O
are	O
usually	O
discouraged	O
from	O
using	O
recursive	O
algorithms	O
or	O
large	O
stack	B-Application
buffers	O
.	O
</s>
