473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

placement new

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
9 3251
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
8
by: elviin | last post by:
Hello. I tried a sample programm using placement new. I am not sure if i understand this technique correctly. I do not mean usage but a real world application. Could anyone to describe some...
5
by: ma740988 | last post by:
I've got a struct which is comprised of POD types. I know in advance where in memory the struct resides. To make a long story short, I'm doing transfers of 16 bit ADC sampled data between...
4
by: sreedhar.cs | last post by:
Hi all, In my application,I want to place a vector in a specific location in shared memory.(a user supplied pointer). I understand that the STL allocator mechanism places the data objects within...
15
by: mangesh | last post by:
This code is from c++ faq in section 11 : void someCode() { char memory; void* p = memory; Fred* f = new(p) Fred(); f->~Fred(); // Explicitly call the destructor for the placed object }
1
by: SarahT | last post by:
Hi folks, I am doing something Very Bad and Wrong (which I'll spare you the details of) that requires overloading new for some specific classes. So, for example: class MyWeirdThingy {...
5
by: Lagarde Sébastien | last post by:
Hello, I write code to debug new call with following macro: #define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-), FALSE) ? NULL : new The setOwner allow to save the...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.