<s>
Stacks	B-Application
in	O
computing	O
architectures	O
are	O
regions	O
of	O
memory	B-General_Concept
where	O
data	O
is	O
added	O
or	O
removed	O
in	O
a	O
last-in-first-out	O
(	O
LIFO	B-Application
)	O
manner	O
.	O
</s>
<s>
In	O
most	O
modern	O
computer	O
systems	O
,	O
each	O
thread	B-Operating_System
has	O
a	O
reserved	O
region	O
of	O
memory	B-General_Concept
referred	O
to	O
as	O
its	O
stack	B-Application
.	O
</s>
<s>
When	O
a	O
function	O
executes	O
,	O
it	O
may	O
add	O
some	O
of	O
its	O
local	O
state	O
data	O
to	O
the	O
top	O
of	O
the	O
stack	B-Application
;	O
when	O
the	O
function	O
exits	O
it	O
is	O
responsible	O
for	O
removing	O
that	O
data	O
from	O
the	O
stack	B-Application
.	O
</s>
<s>
At	O
a	O
minimum	O
,	O
a	O
thread	B-Operating_System
's	O
stack	B-Application
is	O
used	O
to	O
store	O
the	O
location	O
of	O
a	O
return	O
address	O
provided	O
by	O
the	O
caller	O
in	O
order	O
to	O
allow	O
return	O
statements	O
to	O
return	O
to	O
the	O
correct	O
location	O
.	O
</s>
<s>
The	O
stack	B-Application
is	O
often	O
used	O
to	O
store	O
variables	O
of	O
fixed	O
length	O
local	O
to	O
the	O
currently	O
active	O
functions	O
.	O
</s>
<s>
Programmers	O
may	O
further	O
choose	O
to	O
explicitly	O
use	O
the	O
stack	B-Application
to	O
store	O
local	O
data	O
of	O
variable	O
length	O
.	O
</s>
<s>
If	O
a	O
region	O
of	O
memory	B-General_Concept
lies	O
on	O
the	O
thread	B-Operating_System
's	O
stack	B-Application
,	O
that	O
memory	B-General_Concept
is	O
said	O
to	O
have	O
been	O
allocated	O
on	O
the	O
stack	B-Application
,	O
i.e.	O
</s>
<s>
stack-based	B-General_Concept
memory	I-General_Concept
allocation	I-General_Concept
(	O
SBMA	O
)	O
.	O
</s>
<s>
This	O
is	O
contrasted	O
with	O
a	O
heap-based	B-General_Concept
memory	I-General_Concept
allocation	I-General_Concept
(	O
HBMA	O
)	O
.	O
</s>
<s>
The	O
SBMA	O
is	O
often	O
closely	O
coupled	O
with	O
a	O
function	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
Because	O
the	O
data	O
is	O
added	O
and	O
removed	O
in	O
a	O
last-in-first-out	O
manner	O
,	O
stack-based	B-General_Concept
memory	I-General_Concept
allocation	I-General_Concept
is	O
very	O
simple	O
and	O
typically	O
much	O
faster	O
than	O
heap-based	B-General_Concept
memory	I-General_Concept
allocation	I-General_Concept
(	O
also	O
known	O
as	O
dynamic	B-General_Concept
memory	I-General_Concept
allocation	I-General_Concept
)	O
e.g.	O
</s>
<s>
C	B-Language
's	O
.	O
</s>
<s>
Another	O
feature	O
is	O
that	O
memory	B-General_Concept
on	O
the	O
stack	B-Application
is	O
automatically	O
,	O
and	O
very	O
efficiently	O
,	O
reclaimed	O
when	O
the	O
function	O
exits	O
,	O
which	O
can	O
be	O
convenient	O
for	O
the	O
programmer	O
if	O
the	O
data	O
is	O
no	O
longer	O
required	O
.	O
</s>
<s>
(	O
The	O
same	O
applies	O
to	O
longjmp	B-Language
if	O
it	O
moved	O
to	O
a	O
point	O
before	O
the	O
call	O
to	O
happened	O
.	O
)	O
</s>
<s>
If	O
,	O
however	O
,	O
the	O
data	O
needs	O
to	O
be	O
kept	O
in	O
some	O
form	O
,	O
then	O
it	O
must	O
be	O
copied	O
from	O
the	O
stack	B-Application
to	O
the	O
heap	O
before	O
the	O
function	O
exits	O
.	O
</s>
<s>
Therefore	O
,	O
stack	B-Application
based	O
allocation	O
is	O
suitable	O
for	O
temporary	O
data	O
or	O
data	O
which	O
is	O
no	O
longer	O
required	O
after	O
the	O
current	O
function	O
exits	O
.	O
</s>
<s>
A	O
thread	B-Operating_System
's	O
assigned	O
stack	B-Application
size	O
can	O
be	O
as	O
small	O
as	O
only	O
a	O
few	O
bytes	O
on	O
some	O
small	O
CPUs	O
.	O
</s>
<s>
Allocating	O
more	O
memory	B-General_Concept
on	O
the	O
stack	B-Application
than	O
is	O
available	O
can	O
result	O
in	O
a	O
crash	B-General_Concept
due	O
to	O
stack	B-Error_Name
overflow	I-Error_Name
.	O
</s>
<s>
This	O
is	O
also	O
why	O
functions	O
that	O
use	O
are	O
usually	O
prevented	O
from	O
being	O
inlined	O
:	O
should	O
such	O
a	O
function	O
be	O
inlined	O
into	O
a	O
loop	O
,	O
the	O
caller	O
would	O
suffer	O
from	O
an	O
unanticipated	O
growth	O
in	O
stack	B-Application
usage	O
,	O
making	O
an	O
overflow	O
much	O
more	O
likely	O
.	O
</s>
<s>
Stack-based	O
allocation	O
can	O
also	O
cause	O
minor	O
performance	O
problems	O
:	O
it	O
leads	O
to	O
variable-size	O
stack	B-Application
frames	O
,	O
so	O
that	O
both	O
stack	B-Application
and	O
frame	O
pointers	O
need	O
to	O
be	O
managed	O
(	O
with	O
fixed-size	O
stack	B-Application
frames	O
,	O
the	O
stack	B-Application
pointer	O
is	O
redundant	O
due	O
to	O
multiplying	O
the	O
stack	B-Application
frame	O
pointer	O
by	O
the	O
size	O
of	O
each	O
frame	O
)	O
.	O
</s>
<s>
In	O
particular	O
,	O
if	O
the	O
current	O
function	O
contains	O
both	O
calls	O
to	O
alloca	O
and	O
blocks	O
containing	O
variable-length	O
local	O
data	O
then	O
a	O
conflict	O
occurs	O
between	O
alloca	O
's	O
attempts	O
to	O
increase	O
the	O
current	O
stack	B-Application
frame	O
until	O
the	O
current	O
function	O
exits	O
versus	O
the	O
compiler	O
's	O
need	O
to	O
place	O
local	O
variables	O
of	O
variable	O
length	O
in	O
the	O
same	O
location	O
in	O
the	O
stack	B-Application
frame	O
.	O
</s>
<s>
The	O
chain	O
records	O
the	O
stack	B-Application
depth	O
at	O
which	O
each	O
allocation	O
occurs	O
,	O
subsequent	O
calls	O
to	O
alloca	O
in	O
any	O
function	O
trim	O
this	O
chain	O
down	O
to	O
the	O
current	O
stack	B-Application
depth	O
to	O
eventually	O
(	O
but	O
not	O
immediately	O
)	O
free	O
any	O
storage	O
on	O
this	O
chain	O
.	O
</s>
<s>
A	O
call	O
to	O
alloca	O
with	O
an	O
argument	O
of	O
zero	O
can	O
also	O
be	O
used	O
to	O
trigger	O
the	O
freeing	O
of	O
memory	B-General_Concept
without	O
allocating	O
any	O
more	O
such	O
memory	B-General_Concept
.	O
</s>
<s>
As	O
a	O
consequence	O
of	O
this	O
conflict	O
between	O
alloca	O
and	O
local	O
variable	O
storage	O
,	O
using	O
alloca	O
might	O
be	O
no	O
more	O
efficient	O
than	O
using	O
malloc	B-Language
.	O
</s>
<s>
Many	O
Unix-like	B-Operating_System
systems	I-Operating_System
as	O
well	O
as	O
Microsoft	B-Application
Windows	I-Application
implement	O
a	O
function	O
called	O
for	O
dynamically	O
allocating	O
stack	B-General_Concept
memory	I-General_Concept
in	O
a	O
way	O
similar	O
to	O
the	O
heap-based	O
malloc	B-Language
.	O
</s>
<s>
A	O
compiler	O
typically	O
translates	O
it	O
to	O
inlined	O
instructions	O
manipulating	O
the	O
stack	B-Application
pointer	O
,	O
similar	O
to	O
how	O
variable-length	B-Data_Structure
arrays	I-Data_Structure
are	O
handled	O
.	O
</s>
<s>
Although	O
there	O
is	O
no	O
need	O
to	O
explicitly	O
free	O
the	O
memory	B-General_Concept
,	O
there	O
is	O
a	O
risk	O
of	O
undefined	O
behavior	O
due	O
to	O
stack	B-Error_Name
overflow	I-Error_Name
.	O
</s>
<s>
The	O
function	O
was	O
present	O
on	O
Unix	O
systems	O
as	O
early	O
as	O
32/V	B-Operating_System
(	O
1978	O
)	O
,	O
but	O
is	O
not	O
part	O
of	O
Standard	O
C	B-Language
or	O
any	O
POSIX	O
standard	O
.	O
</s>
<s>
A	O
safer	O
version	O
of	O
called	O
,	O
which	O
reports	O
errors	O
,	O
exists	O
on	O
Microsoft	B-Application
Windows	I-Application
.	O
</s>
<s>
gnulib	B-Application
provides	O
an	O
equivalent	O
interface	O
,	O
albeit	O
instead	O
of	O
throwing	O
an	O
SEH	O
exception	O
on	O
overflow	O
,	O
it	O
delegates	O
to	O
when	O
an	O
overlarge	O
size	O
is	O
detected	O
.	O
</s>
<s>
Some	O
processor	O
families	O
,	O
such	O
as	O
the	O
x86	B-Operating_System
,	O
have	O
special	O
instructions	O
for	O
manipulating	O
the	O
stack	B-Application
of	O
the	O
currently	O
executing	O
thread	B-Operating_System
.	O
</s>
<s>
Other	O
processor	O
families	O
,	O
including	O
PowerPC	B-Architecture
and	O
MIPS	B-Device
,	O
do	O
not	O
have	O
explicit	O
stack	B-Application
support	O
,	O
but	O
instead	O
rely	O
on	O
convention	O
and	O
delegate	O
stack	B-Application
management	O
to	O
the	O
operating	O
system	O
's	O
application	B-Operating_System
binary	I-Operating_System
interface	I-Operating_System
(	O
ABI	O
)	O
.	O
</s>
<s>
In	O
addition	O
,	O
since	O
the	O
C	B-Language
version	O
C99	O
(	O
optional	O
since	O
C11	O
)	O
,	O
it	O
is	O
possible	O
to	O
create	O
an	O
array	O
on	O
the	O
stack	B-Application
within	O
a	O
function	O
,	O
automatically	O
,	O
known	O
as	O
an	O
auto	O
VLA	O
(	O
variable-length	B-Data_Structure
array	I-Data_Structure
)	O
.	O
</s>
