By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,042 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,042 IT Pros & Developers. It's quick & easy.

placement new

P: n/a
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.

Sep 10 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
karthikbalaguru wrote:
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 ?
The benefit comes when the allocation does not happen for several
constructions, like when 'std::vector' shrinks and then resizes,
it doesn't deallocate its storage but instead keeps destructing and
construcing elements in place (using placement new).
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 10 '07 #2

P: n/a
On 2007-09-10 20:29, karthikbalaguru 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 ?
Placement new is faster because it does not have to allocate the memory
before it constructs an object, normal new first allocates memory and
then constructs the object. One example of this is std::vector which
allocates more memory than it uses and then constructs new elements into
this memory.

If we assume that all allocations take the same amount of time then
allocating one large chunk of memory and then constructing N objects
into that chunk takes A+N*C time (A is allocation time, and C is
construction time). Making one allocation for each construction takes
N*(A+C) time, which is (N-1)*A longer.
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
}
I'm not an expert on the area but I'm quite sure that it's the other way
around; new first allocates memory and then uses placement new to
construct the object into this memory.

--
Erik Wikström
Sep 10 '07 #3

P: n/a
On 10 Zá , 20:29, karthikbalaguru <karthikbalagur...@gmail.comwrote:
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.

Sep 10 '07 #4

P: n/a
On Sep 11, 12:09 am, Ondra Holub <ondra.ho...@post.czwrote:
On 10 Zá , 20:29, karthikbalaguru <karthikbalagur...@gmail.comwrote:


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 ?

Thx in advans,
Karthik Balaguru

Sep 10 '07 #5

P: n/a
karthikbalaguru <ka***************@gmail.comwrote in
news:11**********************@o80g2000hse.googlegr oups.com:
Hi,

I find that articles stating that 'placement new' constructs an object
on a pre-allocated buffer and so takes less time.
Depends on what you're trying to measure. Yes, 'placement new' is
faster than a simple 'new'. But that's because 'new' has to do more
work.
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 ?
That's a different question. And it might be faster and it might be
slower. Depends on what you're doing to allocate the buffer. Your own
allocator may or may not be faster than the "default" allocator.
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 ?
Because you're not looking at the whole question. If you already have
the memory block through some other mechanism, then you don't need to
spend time talking to the global allocator to get more memory.
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
No.. the pre-allocated buffer may not be in heap.... neither does it
necessarily come from new.
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' ?
How about: I want to construct an object in a shared memory segment (Ok,
this bit is somewhat offtopic). I can't use the normal new since it
isn't allocated off of the heap. So I use whatever the allocation
method is for the shared memory segment and then use placement new to
construct the object in that memory block.

Or look at many implementations of vector. It will have allocated some
chunk of memory for all of the current objects in the vector, plus some
more for some future objects (Probably. This is why capacity() doesn't
necessarily equal size().). When you go and push_back() another object
into the vector, all vector needs to do is use a placement new on the
next "empty" space in the vector to copy-construct the object in place.
vector doesn't need to get more memory, it already has it. (Not
counting the case where capacity() == size() before the push_back. Then
the vector has to go get a new bigger space to hold the vector, copy
over all of the existing objects, and then placement new the next
object. And delete the previous memory.).

Sep 10 '07 #6

P: n/a
On 2007-09-10 21:28, karthikbalaguru wrote:
On Sep 11, 12:09 am, Ondra Holub <ondra.ho...@post.czwrote:
>On 10 Zá , 20:29, karthikbalaguru <karthikbalagur...@gmail.comwrote:


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 ?
It is used every time you write something like

Foo* f = new Foo();

Read http://www.parashift.com/c++-faq-lit...html#faq-16.10
to see how (line 4 in the code).

--
Erik Wikström
Sep 10 '07 #7

P: n/a
karthikbalaguru <ka***************@gmail.comwrote in
news:11**********************@o80g2000hse.googlegr oups.com:

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 ?
You're using a rather vague term. What is "efficient"? Efficient in terms
of what? And recall all of the standard admonitions about "Premature
optimization is the root of all evil.". Measure first, then worry about
optimizing.
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 ?
I think you already have drawn a conclusion and are looking for arguments
to support it instead of examining the entire issue impartially.
So, practically, what is the need of 'placement new' ? Any ideas ?
You already have at least one answer to this question. std::vector.
Sep 10 '07 #8

P: n/a
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<MyCoolTypev;
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.
Sep 11 '07 #9

P: n/a
On Sep 11, 12:56 am, Andre Kostur <nntps...@kostur.netwrote:
karthikbalaguru <karthikbalagur...@gmail.comwrote innews:11**********************@o80g2000hse.google groups.com:
Hi,
I find that articles stating that 'placement new' constructs an object
on a pre-allocated buffer and so takes less time.

Depends on what you're trying to measure. Yes, 'placement new' is
faster than a simple 'new'. But that's because 'new' has to do more
work.
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 ?

That's a different question. And it might be faster and it might be
slower. Depends on what you're doing to allocate the buffer. Your own
allocator may or may not be faster than the "default" allocator.


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 ?

Because you're not looking at the whole question. If you already have
the memory block through some other mechanism, then you don't need to
spend time talking to the global allocator to get more memory.
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

No.. the pre-allocated buffer may not be in heap.... neither does it
necessarily come from new.
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' ?

How about: I want to construct an object in a shared memory segment (Ok,
this bit is somewhat offtopic). I can't use the normal new since it
isn't allocated off of the heap. So I use whatever the allocation
method is for the shared memory segment and then use placement new to
construct the object in that memory block.

Or look at many implementations of vector. It will have allocated some
chunk of memory for all of the current objects in the vector, plus some
more for some future objects (Probably. This is why capacity() doesn't
necessarily equal size().). When you go and push_back() another object
into the vector, all vector needs to do is use a placement new on the
next "empty" space in the vector to copy-construct the object in place.
vector doesn't need to get more memory, it already has it. (Not
counting the case where capacity() == size() before the push_back. Then
the vector has to go get a new bigger space to hold the vector, copy
over all of the existing objects, and then placement new the next
object. And delete the previous memory.).- Hide quoted text -

- Show quoted text -
Interesting !! Thx for the info.

Karthik Balaguru

Sep 11 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.