On Sep 11, 12:09 am, Ondra Holub <ondra.ho...@post.cz> wrote:
On 10 Z? , 20:29, karthikbalaguru <karthikbalagur...@gmail.com> wrote:
Hi,
I find that articles stating that 'placement new' constructs an object
on a pre-allocated buffer and so takes less time.
Actually, we have to consider the allocation of the buffer and then
the construction of object using 'placement new'. So, Effectively it
should be taking more time . What do you think ?
I think,
'placement new' time = Time for creation of pre-allocated buffer +
Time for doing the allocation in
pre-allocated buffer using
'placement new'.
Ex :
void placementnew{
char *buf = new char[1000]; // pre-allocated buffer
string *p = new(buf)string("hi"); // placement new in turn depends
on buf
}
So, i think, it should be as below :
'placement new' time(p) = Time for creation of pre-allocated buffer
called 'buf' + Time for doing the
allocation in
pre-allocated buffer
using 'placement
new' for 'hi'.
So, How could 'placement new' be stated to be faster ?
Further, what is the advantage in using 'placement new'. It in turn is
depending on the pre-allocated buffer in heap that is assigned using
'new'. So, how does 'placement new' prove to be good in comparison
with 'new' as it indirectly depends on heap ?
Why was 'placement new' introduced in c++ ? What are the practical
areas of application of 'placement new' ?
Thx in advans,
Karthik Balaguru.
Hi.
You can preallocate big buffer once and then use many placement new
operators. You can this way get better performance and less fragmented
memory (especially when allocating small amounts of memory). However
you have to manage this part of memory on your own, so what you gain
depends on quality how is your preallocated buffer managed.- Hide quoted text -
Managing Buffers on our own - This sounds something like array
management :(:(
There is every possibility that the Buffer can be manipulated as it is
accessible under a variable name for a programmer. So, 'placement new'
is inturn increasing the burden on the programmer by making the
programmer to take special care of that buffer . What do you think ?
But, normal 'new' does not make the programmer to take care of these.
So, 'new' appears to be
efficient than 'placement new' here . What do you think ?
Maybe time-wise, when construcing large number of objects into a
chunk, 'placement new' appears to be better. But, that better timing
over the normal 'new' does not matter as lot of time will be consumed
to design in such a way to avoid the corruption of buffer(buffer
management) and to fix the errors due to improper buffer management .
What do you think ?
So, practically, what is the need of 'placement new' ? Any ideas ?
to see how (line 4 in the code).