Re: placement new
karthikbalaguru wrote:
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 :(:(
Placement new does _not_ manage memory for you. That's what makes it
different from the regular operator new. Placement new just constructs the
object into the memory you give it, while the non-placement new allocates
that memory. Managing memory on your own might be faster, but probably only
in special cases. The default memory managers are typically optimized very
well for general use.
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 ?
Well, what do you expect to gain from it?
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 ?
There are different kinds of "efficient".
Maybe time-wise, when construcing large number of objects into a
chunk, 'placement new' appears to be better.
I don't think so, unless you want to allocate the chunk first, and then some
time later, construct the objects.
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 ?
You are right about that observation. Also, after all that additional work,
you might find out that your own memory management isn't much faster, or
even that it's slower than the built-in memory manager.
This also smells like the "premature optimization" someone already
mentioned. Do you currently have any performance issues, and did you
determine those to be due to operator new being slow? If not, why bother?
So, practically, what is the need of 'placement new' ? Any ideas ?
Placement new is about seperating memory allocation from object
construction. As an example, std::vector does that. It can allocate a chunk
of memory, and then, some time later, construct objects into that memory.
For example:
std::vector<MyCoolType> v;
v.reserve(1000);
v.push_back(MyCoolType("I'm very cool"));
This code reserves memory for 1000 objects, which means that the vector will
allocate enough space for at least those 1000 objects, but it won't
construct any actual objects. The push_back call copy-constructs its
argument into the reserved space, internally using placement new to do
that. push_back can automatically increase the size if needed, so the
reseve wouldn't strictly be needed for it to work. But even if you don't
reseve(), the vector will typically resize its reserved memory in larger
steps than single objects. Without placement new, it would have to
re-allocate the buffer (i.e. allocate a new one, copy everything over,
destroy the old one) for every single added object, since there would be no
way to separate allocation from construction.