In C++ placement new is used to construct an object at a particular memory location or to pass additional arguments to an allocation function.
Questions tagged [placement-new]
391 questions
512
votes
25 answers
What uses are there for "placement new"?
Has anyone here ever used C++'s "placement new"? If so, what for? It looks to me like it would only be useful on memory-mapped hardware.

Head Geek
- 38,128
- 22
- 77
- 87
71
votes
3 answers
placement new and delete
What is the right method to delete all the memory allocated here?
const char* charString = "Hello, World";
void *mem = ::operator new(sizeof(Buffer) + strlen(charString) + 1);
Buffer* buf = new(mem) Buffer(strlen(charString));
delete…

Vink
- 1,019
- 2
- 9
- 18
68
votes
7 answers
Array placement-new requires unspecified overhead in the buffer?
5.3.4 [expr.new] of the C++11 Feb draft gives the example:
new(2,f) T[5] results in a call of operator new[](sizeof(T)*5+y,2,f).
Here, x and y are non-negative unspecified values representing array allocation overhead; the result of the…

Mooing Duck
- 64,318
- 19
- 100
- 158
68
votes
6 answers
Why isn't there a std::construct_at in C++17?
C++17 adds std::destroy_at, but there isn't any std::construct_at counterpart. Why is that? Couldn't it be implemented as simply as the following?
template
T* construct_at(void* addr, Args&&... args) {
return new…

Daniel Langr
- 22,196
- 3
- 50
- 93
61
votes
9 answers
C++, is it possible to call a constructor directly, without new?
Can I call constructor explicitly, without using new, if I already have a memory for object?
class Object1{
char *str;
public:
Object1(char*str1){
str=strdup(str1);
puts("ctor");
puts(str);
}
~Object1(){
…

osgx
- 90,338
- 53
- 357
- 513
58
votes
5 answers
Why is there a memory leak in this program and how can I solve it, given the constraints (using malloc and free for objects containing std::string)?
This is a minimal working example for the problem I am facing in my real code.
#include
namespace Test1 {
static const std::string MSG1="Something really big message";
}
struct Person{
std::string name;
};
int main() {
auto…

Anurag Vohra
- 1,781
- 12
- 28
44
votes
1 answer
Passing null pointer to placement new
The default placement new operator is declared in 18.6 [support.dynamic] ¶1 with a non-throwing exception-specification:
void* operator new (std::size_t size, void* ptr) noexcept;
This function does nothing except return ptr; so it is reasonable…

Jonathan Wakely
- 166,810
- 27
- 341
- 521
42
votes
8 answers
Malloc and constructors
Unlike new and delete expressions, std::malloc does not call the constructor when memory for an object is allocated. In that case, how must we create an object so that the constructor will also be called?

ckv
- 10,539
- 20
- 100
- 144
38
votes
3 answers
Is "rebinding" references in C++ like this legal?
Is the following legal in C++?
As far as I can tell, Reference has a trivial destructor, so it should be legal.
But I thought references can't be rebound legally... can they?
template
struct Reference
{
T &r;
Reference(T &r) : r(r)…

user541686
- 205,094
- 128
- 528
- 886
37
votes
11 answers
malloc & placement new vs. new
I've been looking into this for the past few days, and so far I haven't really found anything convincing other than dogmatic arguments or appeals to tradition (i.e. "it's the C++ way!").
If I'm creating an array of objects, what is the compelling…

Robert Allan Hennigan Leahy
- 6,478
- 28
- 45
33
votes
4 answers
How to properly free the memory allocated by placement new?
I've been reading somewere that when you use placement new then you have to call the destructor manually.
Consider the folowing code:
// Allocate memory ourself
char* pMemory = new char[ sizeof(MyClass)];
// Construct the object ourself
MyClass*…

codekiddy
- 5,897
- 9
- 50
- 80
31
votes
6 answers
What is an in-place constructor in C++?
Possible Duplicate:
C++'s “placement new”
What is an in-place constructor in C++?
e.g. Datatype *x = new(y) Datatype();

Akhil
- 2,269
- 6
- 32
- 39
29
votes
6 answers
Destroy and then construct new object using the same variable
Sometimes it's nice to start over. In C++ I can employ this following simple manoeuvre:
{
T x(31, Blue, false);
x.~T(); // enough with the old x
::new (&x) T(22, Brown, true); // in with the new!
//…

Kerrek SB
- 464,522
- 92
- 875
- 1,084
28
votes
3 answers
Why can't constructors be explicitly called while destructors can?
In the following C++ code, I am allowed to explicitly call the destructor but not the constructor. Why is that? Wouldn't be explicit ctor call more expressive and unified with the dtor case?
class X { };
int main() {
X* x = (X*)::operator…

Daniel Langr
- 22,196
- 3
- 50
- 93
27
votes
2 answers
Constexpr placement new?
The C++ standard specifically bans calling new in a constant expression (N4296 section 5.20 [expr.const]):
A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would…

Tristan Brindle
- 16,281
- 4
- 39
- 82