Re: Passing address of stack memory to placement new operator
This code is from c++ faq in section 11 :
void* p = memory;
Fred* f = new(p) Fred();
Fred* f = ::new(p) Fred();
f->~Fred(); // Explicitly call the destructor for the placed
Here we r passing address of stack memory to new .
New is used to allocate memory on heap . Then how can we pass address
stack memory to new operator . It is confusing .
The basic placement new operator (there are an infinity of them, but the
basic one from <new>) constructs an object in a specified region of memory.
You shouldn't use it because it's a low-level mechanism to subvert the
ordinary language rules and as such is fraught with dangers, even more
than with casts, e.g., as just one example, here that 'memory' may not
be properly aligned for an object of type 'Fred', and in particular:
* A novice should /never/ use the basic placement new.
Most uses of (the basic) placement new are better expressed using
standard library classes such as std::vector, which do the dangerous
stuff for you, in a safe way, so that you don't even see it.
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
Q: What is the most annoying thing on usenet and in e-mail?