<s>
In	O
the	O
C++	B-Language
programming	I-Language
language	I-Language
,	O
placement	B-Language
syntax	I-Language
allows	O
programmers	O
to	O
explicitly	O
specify	O
the	O
memory	B-General_Concept
management	I-General_Concept
of	O
individual	O
objects	O
—	O
i.e.	O
</s>
<s>
their	O
"	O
placement	O
"	O
in	O
memory	B-General_Concept
.	O
</s>
<s>
Normally	O
,	O
when	O
an	O
object	O
is	O
created	O
dynamically	O
,	O
an	O
allocation	O
function	O
is	O
invoked	O
in	O
such	O
a	O
way	O
that	O
it	O
will	O
both	O
allocate	O
memory	B-General_Concept
for	O
the	O
object	O
,	O
and	O
initialize	O
the	O
object	O
within	O
the	O
newly	O
allocated	O
memory	B-General_Concept
.	O
</s>
<s>
The	O
placement	B-Language
syntax	I-Language
allows	O
the	O
programmer	O
to	O
supply	O
additional	O
arguments	O
to	O
the	O
allocation	O
function	O
.	O
</s>
<s>
A	O
common	O
use	O
is	O
to	O
supply	O
a	O
pointer	O
to	O
a	O
suitable	O
region	O
of	O
storage	O
where	O
the	O
object	O
can	O
be	O
initialized	O
,	O
thus	O
separating	O
memory	B-General_Concept
allocation	I-General_Concept
from	O
object	O
construction	O
.	O
</s>
<s>
The	O
"	O
placement	O
"	O
versions	O
of	O
the	O
new	B-Language
and	I-Language
delete	I-Language
operators	O
and	O
functions	O
are	O
known	O
as	O
placement	B-Language
new	I-Language
and	O
placement	B-Language
delete	I-Language
.	O
</s>
<s>
A	O
new	B-Language
expression	O
,	O
placement	O
or	O
otherwise	O
,	O
calls	O
a	O
new	B-Language
function	O
,	O
also	O
known	O
as	O
an	O
allocator	B-Application
function	O
,	O
whose	O
name	O
is	O
operator	O
new	B-Language
.	O
</s>
<s>
Similarly	O
,	O
a	O
delete	B-Language
expression	O
calls	O
a	O
delete	B-Language
function	O
,	O
also	O
known	O
as	O
a	O
deallocator	O
function	O
,	O
whose	O
name	O
is	O
operator	B-Language
delete	I-Language
.	O
</s>
<s>
Any	O
new	B-Language
expression	O
that	O
uses	O
the	O
placement	B-Language
syntax	I-Language
is	O
a	O
placement	B-Language
new	I-Language
expression	O
,	O
and	O
any	O
operator	O
new	B-Language
or	O
operator	B-Language
delete	I-Language
function	O
that	O
takes	O
more	O
than	O
the	O
mandatory	O
first	O
parameter	O
(	O
)	O
is	O
a	O
placement	B-Language
new	I-Language
or	O
placement	B-Language
delete	I-Language
function	O
.	O
</s>
<s>
A	O
placement	B-Language
new	I-Language
function	O
takes	O
two	O
input	O
parameters	O
:	O
and	O
.	O
</s>
<s>
In	O
earlier	O
versions	O
of	O
C++	B-Language
there	O
was	O
no	O
such	O
thing	O
as	O
placement	B-Language
new	I-Language
;	O
instead	O
,	O
developers	O
used	O
explicit	O
assignment	O
to	O
this	O
within	O
constructors	O
to	O
achieve	O
similar	O
effect	O
.	O
</s>
<s>
This	O
practice	O
has	O
been	O
deprecated	O
and	O
abolished	O
later	O
,	O
and	O
the	O
third	O
edition	O
of	O
The	O
C++	B-Language
Programming	I-Language
Language	I-Language
does	O
n't	O
mention	O
this	O
technique	O
.	O
</s>
<s>
The	O
placement	B-Language
syntax	I-Language
adds	O
an	O
expression	O
list	O
immediately	O
after	O
the	O
new	B-Language
keyword	O
.	O
</s>
<s>
The	O
placement	B-Language
new	I-Language
functions	O
are	O
overloads	O
of	O
the	O
non-placement	O
new	O
functions	O
.	O
</s>
<s>
The	O
declaration	O
of	O
the	O
non-placement	O
new	B-Language
functions	O
,	O
for	O
non-array	O
and	O
array	O
new	B-Language
expressions	O
respectively	O
,	O
are	O
:	O
</s>
<s>
The	O
Standard	O
C++	B-Language
library	O
provides	O
two	O
placement	O
overloads	O
each	O
for	O
these	O
functions	O
.	O
</s>
<s>
In	O
all	O
of	O
the	O
overloads	O
,	O
the	O
first	O
parameter	O
to	O
the	O
operator	O
new	B-Language
function	O
is	O
of	O
type	O
,	O
which	O
when	O
the	O
function	O
is	O
called	O
will	O
be	O
passed	O
as	O
an	O
argument	O
specifying	O
the	O
amount	O
of	O
memory	B-General_Concept
,	O
in	O
bytes	O
,	O
to	O
allocate	O
.	O
</s>
<s>
There	O
are	O
also	O
placement	B-Language
delete	I-Language
functions	O
.	O
</s>
<s>
The	O
Standard	O
C++	B-Language
library	O
provides	O
two	O
placement	O
overloads	O
each	O
for	O
these	O
functions	O
.	O
</s>
<s>
In	O
all	O
of	O
the	O
overloads	O
,	O
the	O
first	O
parameter	O
to	O
the	O
operator	B-Language
delete	I-Language
function	O
is	O
of	O
type	O
,	O
which	O
is	O
the	O
address	O
of	O
the	O
storage	O
to	O
deallocate	O
.	O
</s>
<s>
For	O
both	O
the	O
new	B-Language
and	O
the	O
delete	B-Language
functions	O
,	O
the	O
functions	O
are	O
global	O
,	O
are	O
not	O
in	O
any	O
namespace	O
,	O
and	O
do	O
not	O
have	O
static	O
linkage	O
.	O
</s>
<s>
Placement	B-Language
syntax	I-Language
has	O
four	O
main	O
uses	O
:	O
default	O
placement	O
,	O
preventing	O
exceptions	O
,	O
custom	O
allocators	B-Application
,	O
and	O
debugging	O
.	O
</s>
<s>
The	O
placement	O
overloads	O
of	O
operator	O
new	B-Language
and	O
operator	B-Language
delete	I-Language
that	O
employ	O
an	O
additional	O
parameter	O
are	O
used	O
for	O
default	O
placement	O
,	O
also	O
known	O
as	O
pointer	O
placement	O
.	O
</s>
<s>
Their	O
definitions	O
by	O
the	O
Standard	O
C++	B-Language
library	O
,	O
which	O
it	O
is	O
not	O
permitted	O
for	O
a	O
C++	B-Language
program	I-Language
to	O
replace	O
or	O
override	O
,	O
are	O
:	O
</s>
<s>
Bjarne	O
Stroustrup	O
originally	O
observed	O
,	O
in	O
his	O
book	O
The	O
Design	O
and	O
Evolution	O
of	O
C++	B-Language
,	O
that	O
pointer	O
placement	B-Language
new	I-Language
is	O
necessary	O
for	O
hardware	O
that	O
expects	O
a	O
certain	O
object	O
at	O
a	O
specific	O
hardware	O
address	O
.	O
</s>
<s>
It	O
is	O
also	O
required	O
for	O
the	O
construction	O
of	O
objects	O
that	O
need	O
to	O
reside	O
in	O
a	O
certain	O
memory	B-General_Concept
area	O
,	O
such	O
as	O
an	O
area	O
that	O
is	O
shared	O
between	O
several	O
processors	O
of	O
a	O
multiprocessor	O
computer	O
.	O
</s>
<s>
Other	O
uses	O
,	O
however	O
,	O
include	O
calling	O
a	O
constructor	O
directly	O
,	O
something	O
which	O
the	O
C++	B-Language
language	I-Language
does	O
not	O
otherwise	O
permit	O
.	O
</s>
<s>
The	O
C++	B-Language
language	I-Language
does	O
allow	O
a	O
program	O
to	O
call	O
a	O
destructor	B-Language
directly	O
,	O
and	O
,	O
since	O
it	O
is	O
not	O
possible	O
to	O
destroy	O
the	O
object	O
using	O
a	O
delete	B-Language
expression	O
,	O
that	O
is	O
how	O
one	O
destroys	O
an	O
object	O
that	O
was	O
constructed	O
via	O
a	O
pointer	O
placement	B-Language
new	I-Language
expression	O
.	O
</s>
<s>
Placement	B-Language
new	I-Language
is	O
used	O
when	O
you	O
do	O
not	O
want	O
operator	O
new	B-Language
to	O
allocate	O
memory	B-General_Concept
(	O
you	O
have	O
pre-allocated	O
it	O
and	O
you	O
want	O
to	O
place	O
the	O
object	O
there	O
)	O
,	O
but	O
you	O
do	O
want	O
the	O
object	O
to	O
be	O
constructed	O
.	O
</s>
<s>
You	O
want	O
to	O
create	O
objects	O
in	O
memory	B-General_Concept
shared	O
between	O
two	O
different	O
processes	O
.	O
</s>
<s>
You	O
want	O
objects	O
to	O
be	O
created	O
in	O
non-pageable	O
memory	B-General_Concept
.	O
</s>
<s>
You	O
want	O
to	O
separate	O
memory	B-General_Concept
allocation	I-General_Concept
from	O
construction	O
e.g.	O
</s>
<s>
The	O
basic	O
problem	O
is	O
that	O
the	O
constructor	O
is	O
a	O
peculiar	O
function	O
;	O
when	O
it	O
starts	O
off	O
,	O
there	O
is	O
no	O
object	O
,	O
only	O
raw	O
memory	B-General_Concept
.	O
</s>
<s>
The	O
solution	O
is	O
the	O
placement	O
form	O
of	O
operator	O
new	B-Language
.	O
</s>
<s>
Normally	O
,	O
the	O
(	O
non-placement	O
)	O
new	B-Language
functions	O
throw	O
an	O
exception	O
,	O
of	O
type	O
std::bad_alloc	O
,	O
if	O
they	O
encounter	O
an	O
error	O
,	O
such	O
as	O
exhaustion	O
of	O
all	O
available	O
memory	B-General_Concept
.	O
</s>
<s>
This	O
was	O
not	O
how	O
the	O
functions	O
were	O
defined	O
by	O
Stroustrup	O
's	O
Annotated	O
C++	B-Language
Reference	O
Manual	O
,	O
but	O
was	O
a	O
change	O
made	O
by	O
the	O
standardization	O
committee	O
when	O
the	O
C++	B-Language
language	I-Language
was	O
standardized	O
.	O
</s>
<s>
The	O
original	O
behaviour	O
of	O
the	O
functions	O
,	O
which	O
was	O
to	O
return	O
a	O
pointer	O
when	O
an	O
error	O
occurred	O
,	O
is	O
accessible	O
via	O
placement	B-Language
syntax	I-Language
.	O
</s>
<s>
Programmers	O
who	O
wish	O
to	O
do	O
this	O
in	O
their	O
programs	O
must	O
include	O
the	O
Standard	O
C++	B-Language
library	O
header	O
<new>	O
in	O
the	O
source	O
code	O
.	O
</s>
<s>
This	O
header	O
declares	O
the	O
global	O
std::nothrow	O
object	O
,	O
which	O
is	O
of	O
type	O
std::nothrow_t	O
(	O
also	O
declared	O
in	O
the	O
header	O
)	O
,	O
which	O
is	O
used	O
to	O
call	O
the	O
overloaded	O
new	B-Language
functions	O
that	O
are	O
declared	O
as	O
taking	O
as	O
their	O
second	O
parameter	O
.	O
</s>
<s>
Placement	B-Language
syntax	I-Language
is	O
also	O
employed	O
for	O
custom	O
allocators	B-Application
.	O
</s>
<s>
This	O
does	O
not	O
use	O
any	O
of	O
the	O
allocator	B-Application
and	O
deallocator	O
functions	O
from	O
the	O
Standard	O
C++	B-Language
library	O
header	O
<new>	O
,	O
but	O
requires	O
that	O
programmers	O
write	O
their	O
own	O
allocation	O
and	O
deallocation	B-General_Concept
functions	O
,	O
overloaded	O
for	O
user-defined	O
types	O
.	O
</s>
<s>
For	O
example	O
,	O
one	O
could	O
define	O
a	O
memory	B-General_Concept
management	I-General_Concept
class	O
as	O
follows	O
:	O
</s>
<s>
And	O
define	O
custom	O
placement	O
allocation	O
and	O
deallocation	B-General_Concept
functions	O
as	O
follows	O
:	O
</s>
<s>
The	O
program	O
would	O
employ	O
the	O
placement	B-Language
syntax	I-Language
to	O
allocate	O
objects	O
using	O
different	O
instances	O
of	O
the	O
A	O
class	O
as	O
follows	O
:	O
</s>
<s>
Because	O
there	O
is	O
no	O
placement	B-Language
delete	I-Language
expression	O
,	O
one	O
cannot	O
use	O
it	O
to	O
invoke	O
the	O
custom	O
deallocator	O
.	O
</s>
<s>
One	O
must	O
either	O
write	O
a	O
destruction	O
function	O
that	O
invokes	O
the	O
custom	O
deallocator	O
,	O
or	O
call	O
the	O
placement	B-Language
delete	I-Language
function	O
directly	O
,	O
as	O
a	O
function	O
call	O
.	O
</s>
<s>
The	O
latter	O
would	O
involve	O
simply	O
writing	O
the	O
destructor	B-Language
invocation	O
and	O
delete	B-Language
function	O
call	O
into	O
the	O
program	O
:	O
</s>
<s>
A	O
common	O
error	O
is	O
to	O
attempt	O
to	O
use	O
a	O
delete	B-Language
expression	O
to	O
delete	B-Language
the	O
object	O
.	O
</s>
<s>
This	O
results	O
in	O
the	O
wrong	O
operator	B-Language
delete	I-Language
function	O
being	O
called	O
.	O
</s>
<s>
The	O
first	O
is	O
to	O
ensure	O
that	O
any	O
custom	O
allocators	B-Application
rely	O
upon	O
the	O
Standard	O
C++	B-Language
library	O
's	O
global	O
,	O
non-placement	O
,	O
operator	O
new	B-Language
,	O
and	O
are	O
thus	O
nothing	O
more	O
than	O
simple	O
wrappers	O
around	O
the	O
C++	B-Language
library	O
's	O
memory	B-General_Concept
management	I-General_Concept
.	O
</s>
<s>
The	O
second	O
is	O
to	O
create	O
new	B-Language
and	I-Language
delete	I-Language
functions	O
for	O
individual	O
classes	O
,	O
and	O
customize	O
memory	B-General_Concept
management	I-General_Concept
via	O
class	O
function	O
members	O
rather	O
than	O
by	O
using	O
the	O
placement	B-Language
syntax	I-Language
.	O
</s>
<s>
Placement	B-Language
new	I-Language
can	O
also	O
be	O
used	O
as	O
a	O
simple	O
debugging	O
tool	O
,	O
to	O
enable	O
programs	O
to	O
print	O
the	O
filename	O
and	O
line	O
number	O
of	O
the	O
source	O
code	O
where	O
a	O
memory	B-General_Concept
allocation	I-General_Concept
has	O
failed	O
.	O
</s>
<s>
This	O
does	O
not	O
require	O
the	O
inclusion	O
of	O
the	O
Standard	O
C++	B-Language
library	O
header	O
<new>	O
,	O
but	O
does	O
require	O
the	O
inclusion	O
of	O
a	O
header	O
that	O
declares	O
four	O
placement	O
functions	O
and	O
a	O
macro	O
replacement	O
for	O
the	O
new	B-Language
keyword	O
that	O
is	O
used	O
in	O
new	B-Language
expressions	O
.	O
</s>
<s>
The	O
custom-written	O
placement	B-Language
new	I-Language
functions	O
would	O
then	O
handle	O
using	O
the	O
supplied	O
file	O
and	O
line	O
number	O
information	O
in	O
the	O
event	O
of	O
an	O
exception	O
.	O
</s>
<s>
As	O
noted	O
above	O
,	O
there	O
is	O
no	O
placement	B-Language
delete	I-Language
expression	O
.	O
</s>
<s>
It	O
is	O
not	O
possible	O
to	O
call	O
any	O
placement	O
operator	B-Language
delete	I-Language
function	O
using	O
a	O
delete	B-Language
expression	O
.	O
</s>
<s>
The	O
placement	B-Language
delete	I-Language
functions	O
are	O
called	O
from	O
placement	B-Language
new	I-Language
expressions	O
.	O
</s>
<s>
In	O
such	O
a	O
circumstance	O
,	O
in	O
order	O
to	O
ensure	O
that	O
the	O
program	O
does	O
not	O
incur	O
a	O
memory	B-Error_Name
leak	I-Error_Name
,	O
the	O
placement	B-Language
delete	I-Language
functions	O
are	O
called	O
.	O
</s>
<s>
A	O
placement	B-Language
new	I-Language
expression	O
first	O
calls	O
the	O
placement	O
operator	O
new	B-Language
function	O
,	O
then	O
calls	O
the	O
constructor	O
of	O
the	O
object	O
upon	O
the	O
raw	O
storage	O
returned	O
from	O
the	O
allocator	B-Application
function	O
.	O
</s>
<s>
If	O
the	O
constructor	O
throws	O
an	O
exception	O
,	O
it	O
is	O
necessary	O
to	O
deallocate	O
that	O
storage	O
before	O
propagating	O
the	O
exception	O
back	O
to	O
the	O
code	O
that	O
executed	O
the	O
placement	B-Language
new	I-Language
expression	O
,	O
and	O
that	O
is	O
the	O
purpose	O
of	O
the	O
placement	B-Language
delete	I-Language
functions	O
.	O
</s>
<s>
The	O
placement	B-Language
delete	I-Language
function	O
that	O
is	O
called	O
matches	O
the	O
placement	B-Language
new	I-Language
function	O
that	O
was	O
invoked	O
by	O
the	O
placement	B-Language
new	I-Language
expression	O
.	O
</s>
<s>
So	O
,	O
for	O
example	O
,	O
if	O
the	O
following	O
code	O
is	O
executed	O
,	O
the	O
placement	B-Language
delete	I-Language
function	O
that	O
is	O
called	O
will	O
be	O
operator	O
delete( void	O
*	O
,	O
const	O
A	O
&	O
)	O
:	O
</s>
<s>
This	O
is	O
why	O
the	O
pointer	O
placement	B-Language
delete	I-Language
functions	O
are	O
defined	O
as	O
no-operations	O
by	O
the	O
Standard	O
C++	B-Language
library	O
.	O
</s>
<s>
Since	O
the	O
pointer	O
placement	B-Language
new	I-Language
functions	O
do	O
not	O
allocate	O
any	O
storage	O
,	O
there	O
is	O
no	O
storage	O
to	O
be	O
deallocated	O
in	O
the	O
event	O
of	O
the	O
object	O
's	O
constructor	O
throwing	O
an	O
exception	O
.	O
</s>
<s>
If	O
no	O
matching	O
placement	B-Language
delete	I-Language
function	O
exists	O
,	O
no	O
deallocation	B-General_Concept
function	O
is	O
called	O
in	O
the	O
event	O
of	O
an	O
exception	O
being	O
thrown	O
by	O
a	O
constructor	O
within	O
a	O
placement	B-Language
new	I-Language
expression	O
.	O
</s>
<s>
There	O
are	O
also	O
some	O
(	O
older	O
)	O
C++	B-Language
implementations	O
that	O
do	O
not	O
support	O
placement	B-Language
delete	I-Language
(	O
which	O
,	O
like	O
the	O
exception-throwing	O
allocator	B-Application
functions	O
,	O
were	O
an	O
addition	O
made	O
to	O
C++	B-Language
when	O
it	O
was	O
standardized	O
)	O
at	O
all	O
.	O
</s>
<s>
In	O
both	O
such	O
situations	O
,	O
an	O
exception	O
being	O
thrown	O
by	O
a	O
constructor	O
when	O
allocating	O
using	O
a	O
custom	O
allocator	B-Application
will	O
result	O
in	O
a	O
memory	B-Error_Name
leak	I-Error_Name
.	O
</s>
<s>
(	O
In	O
the	O
case	O
of	O
the	O
older	O
C++	B-Language
implementations	O
,	O
a	O
memory	B-Error_Name
leak	I-Error_Name
will	O
also	O
occur	O
with	O
non-placement	O
new	O
expressions	O
.	O
)	O
</s>
<s>
Placement	B-Language
new	I-Language
expressions	O
are	O
vulnerable	O
to	O
security	O
exploits	O
.	O
</s>
<s>
In	O
2011	O
,	O
Kundu	O
and	O
Bertino	O
demonstrated	O
some	O
of	O
the	O
exploits	O
on	O
placement	B-Language
new	I-Language
.	O
</s>
<s>
Some	O
of	O
the	O
attacks	O
are	O
buffer	O
overflow	O
attacks	O
,	O
object	O
overflow	O
,	O
selective	O
stackguard	O
overriding	O
,	O
virtual	O
pointer	O
subterfuge	O
,	O
memory	B-General_Concept
misalignment	O
attacks	O
.	O
</s>
