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
13
votes
2 answers
How would I portably implement aligned stack storage in C++03?
In C++03 code, how would I portably implement an unsigned char[sizeof(T)] buffer that has the same size and alignment as that of a given type T?
For example:
template
void test()
{
unsigned char buffer[sizeof(T)]; // <----- how do I…

user541686
- 205,094
- 128
- 528
- 886
13
votes
7 answers
assignment of class with const member
Consider the following code:
struct s
{
const int id;
s(int _id):
id(_id)
{}
};
// ...
vector v; v.push_back(s(1));
I get a compiler error that 'const int id' cannot use default assignment operator.
Q1. Why does push_back()…

Dave
- 690
- 4
- 7
- 16
12
votes
1 answer
Is moving an object into malloc'd memory valid C++?
If memory is allocated with malloc (as opposed to new) and an object is moved into that memory, is that valid C++?
Let's say I allocate memory for an array of n objects of type T, and I have a range of n objects of type T that I want to move into…

Philipp
- 957
- 1
- 6
- 20
12
votes
1 answer
Which union member becomes active after placement new
Regarding this code:
#include
int main()
{
union u {
u() { i = 0; }
~u() {}
int i;
std::string s1;
std::string s2;
} u;
new (&u) std::string{};
}
[intro.object]/2 says that
Objects…

Language Lawyer
- 3,378
- 1
- 12
- 29
12
votes
2 answers
std::launder and strict aliasing rule
Consider this code:
void f(char * ptr)
{
auto int_ptr = reinterpret_cast(ptr); // <---- line of interest
// use int_ptr ...
}
void example_1()
{
int i = 10;
f(reinterpret_cast(&i));
}
void example_2()
{
…

phön
- 1,215
- 8
- 20
12
votes
2 answers
Is it possible to reset reference to another value in C++?
I know generally it's impossible to reset a reference after it's already initialized.
However, I somehow try out the following code and it happens to work on both clang++ and g++.
My question is, is the following a valid (behavior-defined)…

user534498
- 3,926
- 5
- 27
- 52
12
votes
4 answers
Legality of using operator delete on a pointer obtained from placement new
I'm dang certain that this code ought to be illegal, as it clearly won't work, but it seems to be allowed by the C++0x FCD.
class X { /* ... */};
void* raw = malloc(sizeof (X));
X* p = new (raw) X(); // according to the standard, the RHS is a…

Ben Voigt
- 277,958
- 43
- 419
- 720
12
votes
3 answers
Is calling delete on the result of a placement delete which used operator new okay?
If I do
struct MyStruct { ~MyStruct() { } };
void *buffer = operator new(1024);
MyStruct *p = new(buffer) MyStruct();
// ...
delete p; // <---------- is this okay?
is the delete guaranteed to take care of both calling ~MyStruct() as well as…

user541686
- 205,094
- 128
- 528
- 886
11
votes
2 answers
Does casting a pointer to "void*" have any effect when placement new is called?
I'm reviewing code of a custom container and some portions of it create elements like this:
::new( (void*)&buffer[index] ) CStoredType( other );
and some do like this:
::new( &buffer[index] ) CStoredType( other );
So both use placement new to…

sharptooth
- 167,383
- 100
- 513
- 979
11
votes
2 answers
Can I use placement new(this) in operator=?
Background:
I have a complicated class with many variables. I have a sound and tested copy constructor:
Applepie::Applepie( const Applepie ©) :
m_crust(copy.m_crust),
m_filling(copy.m_filling)
{
}
Some of the member variable copy constructors…

jcwenger
- 11,383
- 1
- 53
- 65
11
votes
4 answers
treating memory returned by operator new(sizeof(T) * N) as an array
In C one can allocate dynamic arrays using malloc(sizeof(T) * N) and then use pointer arithmetic to get elements at i offset in this dynamic array.
In C++ one can do similar using operator new() in the same way as malloc() and then placement new…

xor256
- 197
- 9
11
votes
1 answer
Is it OK to discard placement new return value when initializing objects
This question originates from the comment section in this thread, and has also got an answer there. However, I think it is too important to be left in the comment section only. So I made this Q&A for it.
Placement new can be used to initialize…

Lingxi
- 14,579
- 2
- 37
- 93
11
votes
3 answers
Is it legal to use placement new on initialised memory?
I am exploring the possibility of implementing true (partially) immutable data structures in C++. As C++ does not seem to distinguish between a variable and the object that variable stores, the only way to truly replace the object (without…

MrMobster
- 1,851
- 16
- 25
11
votes
4 answers
Is it okay to give a stack object address to placement new?
Ignoring usefulness of such practice. (Though real-life examples are welcome, of course.)
For example, the following program outputs the correct value for a:
#include
using namespace std;
int main()
{
int a = 11111;
int i = 30;
…

Leo Heinsaar
- 3,887
- 3
- 15
- 35
11
votes
7 answers
placement new to defer to a different constructor
Is this safe? I'm not using any virtual functions in my actual implementation, but I'm tempted to believe that even if I was, it would still be safe.
class Foo
{
Foo()
{
// initialize things
}
Foo( int )
{
new…

Jonathan Swinney
- 1,054
- 9
- 29