<s>
C	B-Language
dynamic	I-Language
memory	I-Language
allocation	I-Language
refers	O
to	O
performing	O
manual	B-Application
memory	I-Application
management	I-Application
for	O
dynamic	B-General_Concept
memory	I-General_Concept
allocation	I-General_Concept
in	O
the	O
C	B-Language
programming	I-Language
language	I-Language
via	O
a	O
group	O
of	O
functions	O
in	O
the	B-Language
C	I-Language
standard	I-Language
library	I-Language
,	O
namely	O
,	O
,	O
,	O
and	O
.	O
</s>
<s>
The	O
C++	B-Language
programming	I-Language
language	I-Language
includes	O
these	O
functions	O
;	O
however	O
,	O
the	O
operators	O
and	O
provide	O
similar	O
functionality	O
and	O
are	O
recommended	O
by	O
that	O
language	O
's	O
authors	O
.	O
</s>
<s>
Still	O
,	O
there	O
are	O
several	O
situations	O
in	O
which	O
using	O
new/delete	O
is	O
not	O
applicable	O
,	O
such	O
as	O
garbage	O
collection	O
code	O
or	O
performance-sensitive	O
code	O
,	O
and	O
a	O
combination	O
of	O
malloc	B-Language
and	O
placementnew	O
may	O
be	O
required	O
instead	O
of	O
the	O
higher-level	O
new	O
operator	O
.	O
</s>
<s>
Many	O
different	O
implementations	O
of	O
the	O
actual	O
memory	B-General_Concept
allocation	I-General_Concept
mechanism	O
,	O
used	O
by	O
,	O
are	O
available	O
.	O
</s>
<s>
Their	O
performance	O
varies	O
in	O
both	O
execution	B-Library
time	I-Library
and	O
required	O
memory	O
.	O
</s>
<s>
The	O
C	B-Language
programming	I-Language
language	I-Language
manages	O
memory	O
statically	B-General_Concept
,	O
automatically	B-General_Concept
,	O
or	O
dynamically	B-General_Concept
.	O
</s>
<s>
Static-duration	O
variables	O
are	O
allocated	O
in	O
main	O
memory	O
,	O
usually	O
along	O
with	O
the	O
executable	O
code	O
of	O
the	O
program	O
,	O
and	O
persist	O
for	O
the	O
lifetime	O
of	O
the	O
program	O
;	O
automatic-duration	O
variables	O
are	O
allocated	O
on	O
the	O
stack	B-General_Concept
and	O
come	O
and	O
go	O
as	O
functions	O
are	O
called	O
and	O
return	O
.	O
</s>
<s>
For	O
static-duration	O
and	O
automatic-duration	O
variables	O
,	O
the	O
size	O
of	O
the	O
allocation	O
must	O
be	O
compile-time	B-Application
constant	O
(	O
except	O
for	O
the	O
case	O
of	O
variable-length	O
automatic	O
arrays	O
)	O
.	O
</s>
<s>
If	O
the	O
required	O
size	O
is	O
not	O
known	O
until	O
run-time	B-Library
(	O
for	O
example	O
,	O
if	O
data	O
of	O
arbitrary	O
size	O
is	O
being	O
read	O
from	O
the	O
user	O
or	O
from	O
a	O
disk	O
file	O
)	O
,	O
then	O
using	O
fixed-size	O
data	O
objects	O
is	O
inadequate	O
.	O
</s>
<s>
These	O
limitations	O
are	O
avoided	O
by	O
using	O
dynamic	B-General_Concept
memory	I-General_Concept
allocation	I-General_Concept
,	O
in	O
which	O
memory	O
is	O
more	O
explicitly	O
(	O
but	O
more	O
flexibly	O
)	O
managed	O
,	O
typically	O
by	O
allocating	O
it	O
from	O
the	O
an	O
area	O
of	O
memory	O
structured	O
for	O
this	O
purpose	O
.	O
</s>
<s>
In	O
C	B-Language
,	O
the	O
library	O
function	O
malloc	B-Language
is	O
used	O
to	O
allocate	O
a	O
block	O
of	O
memory	O
on	O
the	O
heap	O
.	O
</s>
<s>
The	O
program	O
accesses	O
this	O
block	O
of	O
memory	O
via	O
a	O
pointer	O
that	O
malloc	B-Language
returns	O
.	O
</s>
<s>
The	O
original	O
description	O
of	O
C	B-Language
indicated	O
that	O
calloc	B-Language
and	O
cfree	O
were	O
in	O
the	O
standard	O
library	O
,	O
but	O
not	O
malloc	B-Language
.	O
</s>
<s>
Code	O
for	O
a	O
simple	O
model	O
implementation	O
of	O
a	O
storage	O
manager	O
for	O
Unix	B-Application
was	O
given	O
with	O
alloc	O
and	O
free	O
as	O
the	O
user	O
interface	O
functions	O
,	O
and	O
using	O
the	O
sbrk	B-General_Concept
system	B-Operating_System
call	I-Operating_System
to	O
request	O
memory	O
from	O
the	O
operating	O
system	O
.	O
</s>
<s>
The	O
6th	O
Edition	O
Unix	B-Application
documentation	O
gives	O
alloc	O
and	O
free	O
as	O
the	O
low-level	O
memory	B-General_Concept
allocation	I-General_Concept
functions	O
.	O
</s>
<s>
The	O
malloc	B-Language
and	O
free	O
routines	O
in	O
their	O
modern	O
form	O
are	O
completely	O
described	O
in	O
the	O
7th	O
Edition	O
Unix	B-Application
manual	O
.	O
</s>
<s>
Some	O
platforms	O
provide	O
library	O
or	O
intrinsic	B-Application
function	I-Application
calls	O
which	O
allow	O
run-time	B-Library
dynamic	B-Application
allocation	I-Application
from	O
the	O
C	B-Language
stack	B-General_Concept
rather	O
than	O
the	O
heap	O
(	O
e.g.	O
</s>
<s>
This	O
memory	O
is	O
automatically	B-General_Concept
freed	O
when	O
the	O
calling	O
function	O
ends	O
.	O
</s>
<s>
The	O
C	B-Language
dynamic	I-Language
memory	I-Language
allocation	I-Language
functions	O
are	O
defined	O
in	O
stdlib.h	B-Language
header	O
(	O
cstdlib	O
header	O
in	O
C++	B-Language
)	O
.	O
</s>
<s>
malloc( )	O
takes	O
a	O
single	O
argument	O
(	O
the	O
amount	O
of	O
memory	O
to	O
allocate	O
in	O
bytes	O
)	O
,	O
while	O
calloc( )	O
takes	O
two	O
arguments	O
the	O
number	O
of	O
elements	O
and	O
the	O
size	O
of	O
each	O
element	O
.	O
</s>
<s>
malloc( )	O
only	O
allocates	O
memory	O
,	O
while	O
calloc( )	O
allocates	O
and	O
sets	O
the	O
bytes	O
in	O
the	O
allocated	O
region	O
to	O
zero	O
.	O
</s>
<s>
Creating	O
an	O
array	B-Data_Structure
of	O
ten	O
integers	O
with	O
automatic	O
scope	O
is	O
straightforward	O
in	O
C	B-Language
:	O
</s>
<s>
However	O
,	O
the	O
size	O
of	O
the	O
array	B-Data_Structure
is	O
fixed	O
at	O
compile	B-Application
time	I-Application
.	O
</s>
<s>
If	O
one	O
wishes	O
to	O
allocate	O
a	O
similar	O
array	B-Data_Structure
dynamically	B-General_Concept
without	O
using	O
a	O
variable-length_array	O
,	O
which	O
is	O
not	O
guaranteed	O
to	O
be	O
supported	O
in	O
all	O
C11	O
implementations	O
,	O
the	O
following	O
code	O
can	O
be	O
used	O
:	O
</s>
<s>
This	O
computes	O
the	O
number	O
of	O
bytes	O
that	O
ten	O
integers	O
occupy	O
in	O
memory	O
,	O
then	O
requests	O
that	O
many	O
bytes	O
from	O
malloc	B-Language
and	O
assigns	O
the	O
result	O
to	O
a	O
pointer	O
named	O
array	B-Data_Structure
(	O
due	O
to	O
C	B-Language
syntax	O
,	O
pointers	O
and	O
arrays	O
can	O
be	O
used	O
interchangeably	O
in	O
some	O
situations	O
)	O
.	O
</s>
<s>
Because	O
malloc	B-Language
might	O
not	O
be	O
able	O
to	O
service	O
the	O
request	O
,	O
it	O
might	O
return	O
a	O
null	O
pointer	O
and	O
it	O
is	O
good	O
programming	O
practice	O
to	O
check	O
for	O
this	O
:	O
</s>
<s>
When	O
the	O
program	O
no	O
longer	O
needs	O
the	O
dynamic	B-Data_Structure
array	I-Data_Structure
,	O
it	O
must	O
eventually	O
call	O
free	O
to	O
return	O
the	O
memory	O
it	O
occupies	O
to	O
the	O
free	O
store	O
:	O
</s>
<s>
The	O
memory	O
set	O
aside	O
by	O
malloc	B-Language
is	O
not	O
initialized	O
and	O
may	O
contain	O
cruft	B-Device
:	O
the	O
remnants	O
of	O
previously	O
used	O
and	O
discarded	O
data	O
.	O
</s>
<s>
After	O
allocation	O
with	O
malloc	B-Language
,	O
elements	O
of	O
the	O
array	B-Data_Structure
are	O
uninitialized	B-Error_Name
variables	I-Error_Name
.	O
</s>
<s>
The	O
command	O
calloc	B-Language
will	O
return	O
an	O
allocation	O
that	O
has	O
already	O
been	O
cleared	O
:	O
</s>
<s>
With	O
realloc	B-Language
we	O
can	O
resize	O
the	O
amount	O
of	O
memory	O
a	O
pointer	O
points	O
to	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
we	O
have	O
a	O
pointer	O
acting	O
as	O
an	O
array	B-Data_Structure
of	O
size	O
and	O
we	O
want	O
to	O
change	O
it	O
to	O
an	O
array	B-Data_Structure
of	O
size	O
,	O
we	O
can	O
use	O
realloc	B-Language
.	O
</s>
<s>
Note	O
that	O
realloc	B-Language
must	O
be	O
assumed	O
to	O
have	O
changed	O
the	O
base	O
address	O
of	O
the	O
block	O
(	O
i.e.	O
</s>
<s>
malloc	B-Language
returns	O
a	O
void	O
pointer	O
(	O
void	O
*	O
)	O
,	O
which	O
indicates	O
that	O
it	O
is	O
a	O
pointer	O
to	O
a	O
region	O
of	O
unknown	O
data	O
type	O
.	O
</s>
<s>
The	O
use	O
of	O
casting	O
is	O
required	O
in	O
C++	B-Language
due	O
to	O
the	O
strong	O
type	O
system	O
,	O
whereas	O
this	O
is	O
not	O
the	O
case	O
in	O
C	B-Language
.	O
One	O
may	O
"	O
cast	O
"	O
(	O
see	O
type	O
conversion	O
)	O
this	O
pointer	O
to	O
a	O
specific	O
type	O
:	O
</s>
<s>
Including	O
the	O
cast	O
may	O
allow	O
a	O
C	B-Language
program	I-Language
or	O
function	O
to	O
compile	O
as	O
C++	B-Language
.	O
</s>
<s>
The	O
cast	O
allows	O
for	O
pre-1989	O
versions	O
of	O
malloc	B-Language
that	O
originally	O
returned	O
a	O
char	O
*	O
.	O
</s>
<s>
Casting	O
can	O
help	O
the	O
developer	O
identify	O
inconsistencies	O
in	O
type	O
sizing	O
should	O
the	O
destination	O
pointer	O
type	O
change	O
,	O
particularly	O
if	O
the	O
pointer	O
is	O
declared	O
far	O
from	O
the	O
malloc( )	O
call	O
(	O
although	O
modern	O
compilers	O
and	O
static	O
analysers	O
can	O
warn	O
on	O
such	O
behaviour	O
without	O
requiring	O
the	O
cast	O
)	O
.	O
</s>
<s>
Under	O
the	O
C	B-Language
standard	O
,	O
the	O
cast	O
is	O
redundant	O
.	O
</s>
<s>
Adding	O
the	O
cast	O
may	O
mask	O
failure	O
to	O
include	O
the	O
header	O
stdlib.h	B-Language
,	O
in	O
which	O
the	O
function	O
prototype	O
for	O
malloc	B-Language
is	O
found	O
.	O
</s>
<s>
In	O
the	O
absence	O
of	O
a	O
prototype	O
for	O
malloc	B-Language
,	O
the	O
C90	O
standard	O
requires	O
that	O
the	O
C	B-Language
compiler	O
assume	O
malloc	B-Language
returns	O
an	O
int	O
.	O
</s>
<s>
On	O
certain	O
architectures	O
and	O
data	O
models	O
(	O
such	O
as	O
LP64	O
on	O
64-bit	O
systems	O
,	O
where	O
long	O
and	O
pointers	O
are	O
64-bit	O
and	O
int	O
is	O
32-bit	O
)	O
,	O
this	O
error	O
can	O
actually	O
result	O
in	O
undefined	B-Language
behaviour	I-Language
,	O
as	O
the	O
implicitly	O
declared	O
malloc	B-Language
returns	O
a	O
32-bit	O
value	O
whereas	O
the	O
actually	O
defined	O
function	O
returns	O
a	O
64-bit	O
value	O
.	O
</s>
<s>
Depending	O
on	O
calling	O
conventions	O
and	O
memory	O
layout	O
,	O
this	O
may	O
result	O
in	O
stack	B-General_Concept
smashing	I-General_Concept
.	O
</s>
<s>
This	O
issue	O
is	O
less	O
likely	O
to	O
go	O
unnoticed	O
in	O
modern	O
compilers	O
,	O
as	O
C99	B-Language
does	O
not	O
permit	O
implicit	O
declarations	O
,	O
so	O
the	O
compiler	O
must	O
produce	O
a	O
diagnostic	O
even	O
if	O
it	O
does	O
assume	O
int	O
return	O
.	O
</s>
<s>
If	O
the	O
type	O
of	O
the	O
pointer	O
is	O
changed	O
at	O
its	O
declaration	O
,	O
one	O
may	O
also	O
need	O
to	O
change	O
all	O
lines	O
where	O
malloc	B-Language
is	O
called	O
and	O
cast	O
.	O
</s>
<s>
The	O
improper	O
use	O
of	O
dynamic	B-General_Concept
memory	I-General_Concept
allocation	I-General_Concept
can	O
frequently	O
be	O
a	O
source	O
of	O
bugs	O
.	O
</s>
<s>
These	O
can	O
include	O
security	O
bugs	O
or	O
program	O
crashes	O
,	O
most	O
often	O
due	O
to	O
segmentation	B-Error_Name
faults	I-Error_Name
.	O
</s>
<s>
Not	O
checking	O
for	O
allocation	O
failures	O
Memory	B-General_Concept
allocation	I-General_Concept
is	O
not	O
guaranteed	O
to	O
succeed	O
,	O
and	O
may	O
instead	O
return	O
a	O
null	O
pointer	O
.	O
</s>
<s>
Using	O
the	O
returned	O
value	O
,	O
without	O
checking	O
if	O
the	O
allocation	O
is	O
successful	O
,	O
invokes	O
undefined	B-Language
behavior	I-Language
.	O
</s>
<s>
This	O
usually	O
leads	O
to	O
crash	O
(	O
due	O
to	O
the	O
resulting	O
segmentation	B-Error_Name
fault	I-Error_Name
on	O
the	O
null	O
pointer	O
dereference	O
)	O
,	O
but	O
there	O
is	O
no	O
guarantee	O
that	O
a	O
crash	O
will	O
happen	O
so	O
relying	O
on	O
that	O
can	O
also	O
lead	O
to	O
problems	O
.	O
</s>
<s>
Logical	O
errors	O
All	O
allocations	O
must	O
follow	O
the	O
same	O
pattern	O
:	O
allocation	O
using	O
malloc	B-Language
,	O
usage	O
to	O
store	O
data	O
,	O
deallocation	B-General_Concept
using	O
free	O
.	O
</s>
<s>
Failures	O
to	O
adhere	O
to	O
this	O
pattern	O
,	O
such	O
as	O
memory	B-General_Concept
usage	I-General_Concept
after	O
a	O
call	O
to	O
free	O
(	O
dangling	B-Error_Name
pointer	I-Error_Name
)	O
or	O
before	O
a	O
call	O
to	O
malloc	B-Language
(	O
wild	B-Error_Name
pointer	I-Error_Name
)	O
,	O
calling	O
free	O
twice	O
(	O
"	O
double	O
free	O
"	O
)	O
,	O
etc.	O
,	O
usually	O
causes	O
a	O
segmentation	B-Error_Name
fault	I-Error_Name
and	O
results	O
in	O
a	O
crash	O
of	O
the	O
program	O
.	O
</s>
<s>
These	O
errors	O
can	O
be	O
transient	O
and	O
hard	O
to	O
debug	O
–	O
for	O
example	O
,	O
freed	O
memory	O
is	O
usually	O
not	O
immediately	O
reclaimed	O
by	O
the	O
OS	O
,	O
and	O
thus	O
dangling	B-Error_Name
pointers	I-Error_Name
may	O
persist	O
for	O
a	O
while	O
and	O
appear	O
to	O
work	O
.	O
</s>
<s>
In	O
addition	O
,	O
as	O
an	O
interface	O
that	O
precedes	O
ANSI	O
C	B-Language
standardization	O
,	O
and	O
its	O
associated	O
functions	O
have	O
behaviors	O
that	O
were	O
intentionally	O
left	O
to	O
the	O
implementation	O
to	O
define	O
for	O
themselves	O
.	O
</s>
<s>
Although	O
both	O
POSIX	O
and	O
the	O
Single	O
Unix	B-Application
Specification	O
require	O
proper	O
handling	O
of	O
0-size	O
allocations	O
by	O
either	O
returning	O
or	O
something	O
else	O
that	O
can	O
be	O
safely	O
freed	O
,	O
not	O
all	O
platforms	O
are	O
required	O
to	O
abide	O
by	O
these	O
rules	O
.	O
</s>
<s>
Among	O
the	O
many	O
double-free	O
errors	O
that	O
it	O
has	O
led	O
to	O
,	O
the	O
2019	O
WhatsApp	B-Application
RCE	O
was	O
especially	O
prominent	O
.	O
</s>
<s>
The	O
implementation	O
of	O
memory	B-General_Concept
management	I-General_Concept
depends	O
greatly	O
upon	O
operating	O
system	O
and	O
architecture	O
.	O
</s>
<s>
Some	O
operating	O
systems	O
supply	O
an	O
allocator	O
for	O
malloc	B-Language
,	O
while	O
others	O
supply	O
functions	O
to	O
control	O
certain	O
regions	O
of	O
data	O
.	O
</s>
<s>
The	O
same	O
dynamic	B-General_Concept
memory	I-General_Concept
allocator	O
is	O
often	O
used	O
to	O
implement	O
both	O
malloc	B-Language
and	O
the	O
operator	O
new	O
in	O
C++	B-Language
.	O
</s>
<s>
Implementation	O
of	O
the	O
allocator	O
is	O
commonly	O
done	O
using	O
the	O
heap	O
,	O
or	O
data	B-General_Concept
segment	I-General_Concept
.	O
</s>
<s>
The	O
heap	O
method	O
suffers	O
from	O
a	O
few	O
inherent	O
flaws	O
,	O
stemming	O
entirely	O
from	O
fragmentation	B-Architecture
.	O
</s>
<s>
Like	O
any	O
method	O
of	O
memory	B-General_Concept
allocation	I-General_Concept
,	O
the	O
heap	O
will	O
become	O
fragmented	O
;	O
that	O
is	O
,	O
there	O
will	O
be	O
sections	O
of	O
used	O
and	O
unused	O
memory	O
in	O
the	O
allocated	O
space	O
on	O
the	O
heap	O
.	O
</s>
<s>
The	O
major	O
problem	O
with	O
this	O
method	O
is	O
that	O
the	O
heap	O
has	O
only	O
two	O
significant	O
attributes	O
:	O
base	O
,	O
or	O
the	O
beginning	O
of	O
the	O
heap	O
in	O
virtual	B-Architecture
memory	I-Architecture
space	O
;	O
and	O
length	O
,	O
or	O
its	O
size	O
.	O
</s>
<s>
The	O
heap	O
can	O
get	O
"	O
stuck	O
"	O
in	O
this	O
position	O
if	O
a	O
small	O
used	O
segment	O
exists	O
at	O
the	O
end	O
of	O
the	O
heap	O
,	O
which	O
could	O
waste	O
any	O
amount	O
of	O
address	B-General_Concept
space	I-General_Concept
.	O
</s>
<s>
On	O
lazy	O
memory	B-General_Concept
allocation	I-General_Concept
schemes	O
,	O
such	O
as	O
those	O
often	O
found	O
in	O
the	O
Linux	O
operating	O
system	O
,	O
a	O
large	O
heap	O
does	O
not	O
necessarily	O
reserve	O
the	O
equivalent	O
system	O
memory	O
;	O
it	O
will	O
only	O
do	O
so	O
at	O
the	O
first	O
write	O
time	O
(	O
reads	O
of	O
non-mapped	O
memory	B-General_Concept
pages	I-General_Concept
return	O
zero	O
)	O
.	O
</s>
<s>
The	O
granularity	O
of	O
this	O
depends	O
on	O
page	B-General_Concept
size	I-General_Concept
.	O
</s>
<s>
Doug	O
Lea	O
has	O
developed	O
the	O
public	O
domain	O
dlmalloc	O
(	O
"	O
Doug	O
Lea	O
's	O
Malloc	B-Language
"	O
)	O
as	O
a	O
general-purpose	O
allocator	O
,	O
starting	O
in	O
1987	O
.	O
</s>
<s>
The	O
GNU	B-Language
C	I-Language
library	I-Language
(	O
glibc	B-Language
)	O
is	O
derived	O
from	O
Wolfram	O
Gloger	O
's	O
ptmalloc	O
(	O
"	O
pthreads	O
malloc	B-Language
"	O
)	O
,	O
a	O
fork	O
of	O
dlmalloc	O
with	O
threading-related	O
improvements	O
.	O
</s>
<s>
Memory	O
on	O
the	O
heap	O
is	O
allocated	O
as	O
"	O
chunks	O
"	O
,	O
an	O
8-byte	O
aligned	B-Application
data	B-General_Concept
structure	I-General_Concept
which	O
contains	O
a	O
header	O
,	O
and	O
usable	O
memory	O
.	O
</s>
<s>
Allocated	O
memory	O
contains	O
an	O
8	O
-	O
or	O
16-byte	O
overhead	O
for	O
the	O
size	O
of	O
the	O
chunk	O
and	O
usage	O
flags	O
(	O
similar	O
to	O
a	O
dope	B-Data_Structure
vector	I-Data_Structure
)	O
.	O
</s>
<s>
Unallocated	O
memory	O
is	O
grouped	O
into	O
"	O
bins	B-Data_Structure
"	O
of	O
similar	O
sizes	O
,	O
implemented	O
by	O
using	O
a	O
double-linked	O
list	O
of	O
chunks	O
(	O
with	O
pointers	O
stored	O
in	O
the	O
unallocated	O
space	O
inside	O
the	O
chunk	O
)	O
.	O
</s>
<s>
Bins	B-Data_Structure
are	O
sorted	O
by	O
size	O
into	O
three	O
classes	O
:	O
</s>
<s>
If	O
there	O
are	O
no	O
free	O
blocks	O
in	O
that	O
bin	B-Data_Structure
,	O
a	O
block	O
from	O
the	O
next	O
highest	O
bin	B-Data_Structure
is	O
split	O
in	O
two	O
.	O
</s>
<s>
For	O
requests	O
of	O
256	O
bytes	O
or	O
above	O
but	O
below	O
the	O
mmap	B-Operating_System
threshold	O
,	O
dlmalloc	O
since	O
v2.8.0	O
use	O
an	O
in-place	O
bitwise	O
trie	O
algorithm	O
(	O
"	O
treebin	O
"	O
)	O
.	O
</s>
<s>
If	O
there	O
is	O
no	O
free	O
space	O
left	O
to	O
satisfy	O
the	O
request	O
,	O
dlmalloc	O
tries	O
to	O
increase	O
the	O
size	O
of	O
the	O
heap	O
,	O
usually	O
via	O
the	O
brk	B-General_Concept
system	B-Operating_System
call	I-Operating_System
.	O
</s>
<s>
This	O
feature	O
was	O
introduced	O
way	O
after	O
ptmalloc	O
was	O
created	O
(	O
from	O
v2.7.x	O
)	O
,	O
and	O
as	O
a	O
result	O
is	O
not	O
a	O
part	O
of	O
glibc	B-Language
,	O
which	O
inherits	O
the	O
old	O
best-fit	O
allocator	O
.	O
</s>
<s>
For	O
requests	O
above	O
the	O
mmap	B-Operating_System
threshold	O
(	O
a	O
"	O
largebin	O
"	O
request	O
)	O
,	O
the	O
memory	O
is	O
always	O
allocated	O
using	O
the	O
mmap	B-Operating_System
system	B-Operating_System
call	I-Operating_System
.	O
</s>
<s>
The	O
mmap	B-Operating_System
method	O
averts	O
problems	O
with	O
huge	O
buffers	O
trapping	O
a	O
small	O
allocation	O
at	O
the	O
end	O
after	O
their	O
expiration	O
,	O
but	O
always	O
allocates	O
an	O
entire	O
page	B-General_Concept
of	O
memory	O
,	O
which	O
on	O
many	O
architectures	O
is	O
4096	O
bytes	O
in	O
size	O
.	O
</s>
<s>
Game	O
developer	O
Adrian	O
Stone	O
argues	O
that	O
,	O
as	O
a	O
boundary-tag	O
allocator	O
,	O
is	O
unfriendly	O
for	O
console	O
systems	O
that	O
have	O
virtual	B-Architecture
memory	I-Architecture
but	O
do	O
not	O
have	O
demand	B-General_Concept
paging	I-General_Concept
.	O
</s>
<s>
This	O
is	O
because	O
its	O
pool-shrinking	O
and	O
growing	O
callbacks	O
(	O
sysmalloc/systrim	O
)	O
cannot	O
be	O
used	O
to	O
allocate	O
and	O
commit	O
individual	O
pages	O
of	O
virtual	B-Architecture
memory	I-Architecture
.	O
</s>
<s>
In	O
the	O
absence	O
of	O
demand	B-General_Concept
paging	I-General_Concept
,	O
fragmentation	B-Architecture
becomes	O
a	O
greater	O
concern	O
.	O
</s>
<s>
Since	O
FreeBSD	B-Operating_System
7.0	O
and	O
NetBSD	B-Device
5.0	O
,	O
the	O
old	O
malloc	B-Language
implementation	O
(	O
phkmalloc	O
by	O
Poul-Henning	O
Kamp	O
)	O
was	O
replaced	O
by	O
,	O
written	O
by	O
Jason	O
Evans	O
.	O
</s>
<s>
In	O
order	O
to	O
avoid	O
lock	O
contention	O
,	O
jemalloc	O
uses	O
separate	O
"	O
arenas	O
"	O
for	O
each	O
CPU	B-General_Concept
.	O
</s>
<s>
OpenBSD	B-Operating_System
's	O
implementation	O
of	O
the	O
malloc	B-Language
function	O
makes	O
use	O
of	O
mmap	B-Operating_System
.	O
</s>
<s>
For	O
requests	O
greater	O
in	O
size	O
than	O
one	O
page	B-General_Concept
,	O
the	O
entire	O
allocation	O
is	O
retrieved	O
using	O
mmap	B-Operating_System
;	O
smaller	O
sizes	O
are	O
assigned	O
from	O
memory	O
pools	O
maintained	O
by	O
malloc	B-Language
within	O
a	O
number	O
of	O
"	O
bucket	O
pages	O
"	O
,	O
also	O
allocated	O
with	O
mmap	B-Operating_System
.	O
</s>
<s>
On	O
a	O
call	O
to	O
free	O
,	O
memory	O
is	O
released	O
and	O
unmapped	O
from	O
the	O
process	O
address	B-General_Concept
space	I-General_Concept
using	O
munmap	O
.	O
</s>
<s>
This	O
system	O
is	O
designed	O
to	O
improve	O
security	O
by	O
taking	O
advantage	O
of	O
the	O
address	B-General_Concept
space	I-General_Concept
layout	O
randomization	O
and	O
gap	O
page	B-General_Concept
features	O
implemented	O
as	O
part	O
of	O
OpenBSD	B-Operating_System
's	O
mmap	B-Operating_System
system	B-Operating_System
call	I-Operating_System
,	O
and	O
to	O
detect	O
use-after-free	B-Error_Name
bugs	O
—	O
as	O
a	O
large	O
memory	B-General_Concept
allocation	I-General_Concept
is	O
completely	O
unmapped	O
after	O
it	O
is	O
freed	O
,	O
further	O
use	O
causes	O
a	O
segmentation	B-Error_Name
fault	I-Error_Name
and	O
termination	O
of	O
the	O
program	O
.	O
</s>
<s>
The	O
GrapheneOS	O
project	O
initially	O
started	O
out	O
by	O
porting	O
OpenBSD	B-Operating_System
's	O
memory	B-General_Concept
allocator	I-General_Concept
to	O
Android	O
's	O
Bionic	O
C	B-Language
Library	I-Language
.	O
</s>
<s>
Hoard	O
is	O
an	O
allocator	O
whose	O
goal	O
is	O
scalable	O
memory	B-General_Concept
allocation	I-General_Concept
performance	O
.	O
</s>
<s>
Like	O
OpenBSD	B-Operating_System
's	O
allocator	O
,	O
Hoard	O
uses	O
mmap	B-Operating_System
exclusively	O
,	O
but	O
manages	O
memory	O
in	O
chunks	O
of	O
64	O
kilobytes	O
called	O
superblocks	O
.	O
</s>
<s>
By	O
allocating	O
only	O
from	O
superblocks	O
on	O
the	O
local	O
per-thread	O
or	O
per-processor	O
heap	O
,	O
and	O
moving	O
mostly-empty	O
superblocks	O
to	O
the	O
global	O
heap	O
so	O
they	O
can	O
be	O
reused	O
by	O
other	O
processors	O
,	O
Hoard	O
keeps	O
fragmentation	B-Architecture
low	O
while	O
achieving	O
near	O
linear	O
scalability	O
with	O
the	O
number	O
of	O
threads	O
.	O
</s>
<s>
An	O
open-source	B-License
compact	O
general-purpose	O
memory	B-General_Concept
allocator	I-General_Concept
from	O
Microsoft	O
Research	O
with	O
focus	O
on	O
performance	O
.	O
</s>
<s>
For	O
large	O
allocations	O
mmap	B-Operating_System
or	O
sbrk	B-General_Concept
can	O
be	O
used	O
.	O
</s>
<s>
,	O
a	O
malloc	B-Language
developed	O
by	O
Google	O
,	O
has	O
garbage-collection	O
for	O
local	O
storage	O
of	O
dead	O
threads	O
.	O
</s>
<s>
The	O
TCMalloc	O
is	O
considered	O
to	O
be	O
more	O
than	O
twice	O
as	O
fast	O
as	O
glibc	B-Language
's	O
ptmalloc	O
for	O
multithreaded	O
programs	O
.	O
</s>
<s>
Operating	B-Operating_System
system	I-Operating_System
kernels	I-Operating_System
need	O
to	O
allocate	O
memory	O
just	O
as	O
application	O
programs	O
do	O
.	O
</s>
<s>
The	O
implementation	O
of	O
malloc	B-Language
within	O
a	O
kernel	B-Operating_System
often	O
differs	O
significantly	O
from	O
the	O
implementations	O
used	O
by	O
C	B-Language
libraries	I-Language
,	O
however	O
.	O
</s>
<s>
For	O
example	O
,	O
memory	O
buffers	O
might	O
need	O
to	O
conform	O
to	O
special	O
restrictions	O
imposed	O
by	O
DMA	B-General_Concept
,	O
or	O
the	O
memory	B-General_Concept
allocation	I-General_Concept
function	O
might	O
be	O
called	O
from	O
interrupt	O
context	O
.	O
</s>
<s>
This	O
necessitates	O
a	O
malloc	B-Language
implementation	O
tightly	O
integrated	O
with	O
the	O
virtual	B-Architecture
memory	I-Architecture
subsystem	O
of	O
the	O
operating	B-Operating_System
system	I-Operating_System
kernel	I-Operating_System
.	O
</s>
<s>
Because	O
malloc	B-Language
and	O
its	O
relatives	O
can	O
have	O
a	O
strong	O
impact	O
on	O
the	O
performance	O
of	O
a	O
program	O
,	O
it	O
is	O
not	O
uncommon	O
to	O
override	O
the	O
functions	O
for	O
a	O
specific	O
application	O
by	O
custom	O
implementations	O
that	O
are	O
optimized	O
for	O
application	O
's	O
allocation	O
patterns	O
.	O
</s>
<s>
The	O
C	B-Language
standard	O
provides	O
no	O
way	O
of	O
doing	O
this	O
,	O
but	O
operating	O
systems	O
have	O
found	O
various	O
ways	O
to	O
do	O
this	O
by	O
exploiting	O
dynamic	O
linking	O
.	O
</s>
<s>
Another	O
,	O
employed	O
by	O
Unix	B-Application
System	I-Application
V.3	O
,	O
is	O
to	O
make	O
malloc	B-Language
and	O
free	O
function	O
pointers	O
that	O
an	O
application	O
can	O
reset	O
to	O
custom	O
functions	O
.	O
</s>
<s>
The	O
most	O
common	O
form	O
on	O
POSIX-like	O
systems	O
is	O
to	O
set	O
the	O
environment	O
variable	O
LD_PRELOAD	O
with	O
the	O
path	O
of	O
the	O
allocator	O
,	O
so	O
that	O
the	O
dynamic	O
linker	O
uses	O
that	O
version	O
of	O
malloc/calloc/free	O
instead	O
of	O
the	O
libc	B-Language
implementation	O
.	O
</s>
<s>
The	O
largest	O
possible	O
memory	O
block	O
malloc	B-Language
can	O
allocate	O
depends	O
on	O
the	O
host	O
system	O
,	O
particularly	O
the	O
size	O
of	O
physical	O
memory	O
and	O
the	O
operating	O
system	O
implementation	O
.	O
</s>
<s>
In	O
the	O
C99	B-Language
standard	I-Language
and	O
later	O
,	O
it	O
is	O
available	O
as	O
the	O
SIZE_MAX	O
constant	O
from	O
<stdint.h>	O
.	O
</s>
<s>
Although	O
not	O
guaranteed	O
by	O
,	O
it	O
is	O
usually	O
2^( CHAR_BIT	O
*	O
sizeof(size_t )	O
)	O
-	O
1	O
.	O
</s>
<s>
On	O
glibc	B-Language
systems	O
,	O
the	O
largest	O
possible	O
memory	O
block	O
malloc	B-Language
can	O
allocate	O
is	O
only	O
half	O
this	O
size	O
,	O
namely	O
2^( CHAR_BIT	O
*	O
sizeof(ptrdiff_t )	O
-	O
1	O
)	O
-	O
1	O
.	O
</s>
<s>
The	O
C	B-Language
library	I-Language
implementations	O
shipping	O
with	O
various	O
operating	O
systems	O
and	O
compilers	O
may	O
come	O
with	O
alternatives	O
and	O
extensions	O
to	O
the	O
standard	O
malloc	B-Language
interface	O
.	O
</s>
<s>
alloca	O
,	O
which	O
allocates	O
a	O
requested	O
number	O
of	O
bytes	O
on	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
No	O
corresponding	O
deallocation	B-General_Concept
function	O
exists	O
,	O
as	O
typically	O
the	O
memory	O
is	O
deallocated	O
as	O
soon	O
as	O
the	O
calling	O
function	O
returns	O
.	O
</s>
<s>
alloca	O
was	O
present	O
on	O
Unix	B-Application
systems	I-Application
as	O
early	O
as	O
32/V	B-Operating_System
(	O
1978	O
)	O
,	O
but	O
its	O
use	O
can	O
be	O
problematic	O
in	O
some	O
(	O
e.g.	O
,	O
embedded	O
)	O
contexts	O
.	O
</s>
<s>
While	O
supported	O
by	O
many	O
compilers	O
,	O
it	O
is	O
not	O
part	O
of	O
the	O
ANSI-C	O
standard	O
and	O
therefore	O
may	O
not	O
always	O
be	O
portable	O
.	O
</s>
<s>
It	O
may	O
also	O
cause	O
minor	O
performance	O
problems	O
:	O
it	O
leads	O
to	O
variable-size	O
stack	B-General_Concept
frames	O
,	O
so	O
that	O
both	O
stack	B-General_Concept
and	O
frame	O
pointers	O
need	O
to	O
be	O
managed	O
(	O
with	O
fixed-size	O
stack	B-General_Concept
frames	O
,	O
one	O
of	O
these	O
is	O
redundant	O
)	O
.	O
</s>
<s>
Larger	O
allocations	O
may	O
also	O
increase	O
the	O
risk	O
of	O
undefined	B-Language
behavior	I-Language
due	O
to	O
a	O
stack	B-Error_Name
overflow	I-Error_Name
.	O
</s>
<s>
C99	B-Language
offered	O
variable-length	B-Data_Structure
arrays	I-Data_Structure
as	O
an	O
alternative	O
stack	B-General_Concept
allocation	O
mechanism	O
however	O
,	O
this	O
feature	O
was	O
relegated	O
to	O
optional	O
in	O
the	O
later	O
C11	O
standard	O
.	O
</s>
<s>
Its	O
allocations	O
are	O
deallocated	O
with	O
free	O
,	O
so	O
the	O
implementation	O
usually	O
needs	O
to	O
be	O
a	O
part	O
of	O
the	O
malloc	B-Language
library	O
.	O
</s>
