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]
325 questions
447
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
- 33,955
- 20
- 72
- 83
66
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
- 56,371
- 16
- 89
- 146
61
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
- 18,256
- 1
- 39
- 74
55
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
- 80,853
- 42
- 303
- 470
43
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
- 153,269
- 21
- 303
- 482
38
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,078
- 25
- 45
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
- 189,354
- 112
- 476
- 821
28
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
- 428,875
- 83
- 813
- 1,025
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
- 18,256
- 1
- 39
- 74
26
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
- 15,036
- 2
- 31
- 79
25
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,217
- 9
- 46
- 73
25
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,019
- 6
- 30
- 36
24
votes
5 answers
Do I really have to worry about alignment when using placement new operator?
I read this When should I worry about alignment? but I am still do not know if I have to worry about not aligned pointer returned by placement new operator - like in this example:
class A {
public:
long double a;
long long b;
A() : a(1.3),…
PiotrNycz
- 20,687
- 7
- 55
- 102
21
votes
7 answers
Using new (this) to reuse constructors
This came up recently in a class for which I am a teaching assistant. We were teaching the students how to do copy constructors in c++, and the students who were originally taught java asked if you can call one constructor from another. I know the…
Brandon Bodnar
- 8,093
- 2
- 33
- 42
20
votes
4 answers
How to delete object constructed via placement new operator?
char * buf = new char[sizeof(T)];
new (buf) T;
T * t = (T *)buf;
//code...
//here I should destruct *t but as it is argument of template and can be
//instantiated via basic types as well (say int) so such code
/*t->~T();*/
//is incorrect (maybe…
Mihran Hovsepyan
- 10,110
- 13
- 57
- 108