473,785 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

efficiency of vector::push_ba ck?

Say I have objects of class C which are fairly large. Then consider:

vector<C> vc;
vc.push_back(C( ));

Naively this would seem to construct a temporary object C(), copy it
into the space owned by the vector, and then delete the temporary object.

I realize this is implementation dependent, but will most modern
compilers optimize away the creation of the temporary object, or is
there a compelling reason why this is not possible?

Thanks,
Mark
Jul 23 '05
17 4173
Mark P wrote:
Say I have objects of class C which are fairly large. Then consider:

vector<C> vc;
vc.push_back(C( ));

Naively this would seem to construct a temporary object C(), copy it
into the space owned by the vector, and then delete the temporary object.

I realize this is implementation dependent, but will most modern
compilers optimize away the creation of the temporary object, or is
there a compelling reason why this is not possible?

Well, push_back() creates a new object inside vector by using the copy constructor. The
argument of push_back is const T &x, and thus in most compilers your argument is created
and destroyed.

However the above does not make any sense, so it would be better if you provided a real
world example so as to provide some optimisation hints.
In other words you can have the same effect to the above by writing:

vector<C> vc(1);
and avoid your temporary construction.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #11
Mark P wrote:

vector<C> vc;
vc.push_back(C( ));

Naively this would seem to construct a temporary object C(), copy it
into the space owned by the vector, and then delete the temporary object.


There's no reason to delete the temporary object. It will be destroyed,
however.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #12
In article <Is************ ****@newssvr13. news.prodigy.co m>,
Mark P <no*@my.real.em ail> wrote:
Say I have objects of class C which are fairly large. Then consider:

vector<C> vc;
vc.push_back(C( ));

Naively this would seem to construct a temporary object C(), copy it
into the space owned by the vector, and then delete the temporary object.

I realize this is implementation dependent, but will most modern
compilers optimize away the creation of the temporary object, or is
there a compelling reason why this is not possible?

Thanks,
Mark


In addition to some of the fine answers you've already received:

In general the compiler does not know where (at what address) the vector
is going to put the C() being push_back'd. During the push_back
execution, the vector will decide where the object needs to be
constructed (maybe space needs to be allocated, maybe not). At this
point the temporary C() will have long since been constructed in order
to initiate push_back execution in the first place.

So no, the copy can not be optimized away today (or at least is not
optimized away in practice today).

All that being said, there is a proposal before the committee to address
this problem: move semantics / rvalue reference:

http://www.open-std.org/jtc1/sc22/wg...2002/n1377.htm
http://www.open-std.org/jtc1/sc22/wg...004/n1690.html
http://www.open-std.org/jtc1/sc22/wg...005/n1770.html
http://www.open-std.org/jtc1/sc22/wg...005/n1771.html

The basic idea is that the class C can have a move constructor which can
execute far more efficiently than a copy constructor by transferring
resources from the source to the target (instead of duplicating
resources). And you can safely move from temporaries with copy syntax
because nobody else has a reference to the temporary in order to detect
a difference.

So expanding on Robert's example:

#include <iostream>
#include <vector>

class C {
public:
// constructors
C() {
std::cerr << "C::C()" << std::endl;
}
// copy semantics
C(const C&) {
std::cerr << "C::C(const C&)" << std::endl;
}
C& operator=(const C&) {
std::cerr << "C::operator=(c onst C&)" << std::endl;
return *this;
}
// move semantics
C(C&&) {
std::cerr << "C::C(C&&)" << std::endl;
}
C& operator=(C&&) {
std::cerr << "C::operator=(C &&)" << std::endl;
return *this;
}
// destructor
~C(void) {
std::cerr << "C::~C(void )" << std::endl;
}
};

int main() {
std::vector<C> vc;
vc.push_back(C( ));
return 0;
}

This will now print out:

C::C()
C::C(C&&)
C::~C(void)
C::~C(void)

I.e. The temporary is still created, but it is move constructed from
instead of copy constructed from to create the C internal to the vector.

In this simple example there is no efficiency gain. However if C held
resources (such as dynamic memory, file references, mutex locks, socket
connections, etc.) then those resources can be transfered out of the
temporary and into the vector by coding C(C&&) appropriately. ~C() will
then be called on the temporary which must then be able to properly deal
with a resource-less object.

vector will also use move semantics instead of copy semantics when
moving around elements internally (e.g. during buffer reallocation).

The status of this proposal within the committee is currently promising,
and Metrowerks has a full implementation of it in a not yet released
compiler/lib.

-Howard
Jul 23 '05 #13
> However if you store raw pointers you will have a memory leak if an
exception gets thrown across the point at which you define vc. Use a
suitable smart pointer instead.


Where can I find out more about this "smart pointer?"
Jul 23 '05 #14
In article <r65he.28$rI1.7 @trnddc02>, Randy <ab***@127.0.0. 1> wrote:
> However if you store raw pointers you will have a memory leak if an
exception gets thrown across the point at which you define vc. Use a
suitable smart pointer instead.


Where can I find out more about this "smart pointer?"


boost::shared_p tr is a good starting point.

http://www.boost.org/libs/smart_ptr/smart_ptr.htm

Your C++ compiler may come with it, but called std::tr1::share d_ptr.

Other copyable smart pointers may work better for you, depending on your
needs. There is no "One True smart pointer".

-Howard
Jul 23 '05 #15
On Fri, 13 May 2005 11:56:30 +0100, Francis Glassborow
<fr*****@robint on.demon.co.uk> wrote:
In article <j0************ *************** *****@4ax.com>, Robert W Hand
<rw****@NOSPAM operamail.com> writes

I always thought that the point of optimization was invisibility to
the user, other than perhaps execution time or program size. It would
be quite surprising to find that the result of a calculation varied
with turning on and off compiler optimization switches.

By requiring the program to print error messages logging the execution
path, the compiler cannot optimize away "everything ".

But C++ gives specific authorisation wrt copy ctors. Under that licence
side effects of a copy ctor are not required to happen (and may also
happen at semi-arbitrary points if the compiler determines that it
wishes to create a temporary copy)


Yes, 12.8 allows class copy constructors to be the exception. But as
I understand it, the side-effects would occur only if the copy
contructor was actually called. It appears that in this example, the
side effects did occur indicating that the objects were created and
destroyed. Otherwise, how could nothing (no object, remember) have
effects?

12.8 has this language.

"...the implementation treats the source and target of the omitted
copy operation as simply two different ways of referring to the same
object, and the destruction of that object occurs at the later of the
times when the two objects would have been destroyed without the
optimization"

I've always read this phrase to mean that the side-effects of the
eliminated copy and destruction would not occur. What would be the
point of the elision if eerie things happened? (I wrote eerie because
these unborn objects are having effects much some ghost. Which leads
us completely off-topic. Can ghosts be unborn, or must they always be
dead?)

You are right about the temporary copy issue. But you would not want
to have substantially different behavior between implementations
because one compiler wants a temporary and another does not.
--

Best wishes,

Bob
Jul 23 '05 #16
In article <jl************ *************** *****@4ax.com>, Robert W Hand
<rw****@NOSPAMo peramail.com> writes
You are right about the temporary copy issue. But you would not want
to have substantially different behavior between implementations
because one compiler wants a temporary and another does not.
But the burden is on the programmer not the implementation.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Jul 23 '05 #17
Francis Glassborow wrote:
In article <11************ *********@f14g2 000cwb.googlegr oups.com>,
Jonathan Mcdougall <jo************ ***@gmail.com> writes
If constructing and copying objects of class C is
expensive, do not store them by value.
std::vector is usually implemented using a
dynamic array, so when it has to allocate memory,
it will copy its elements. Use pointers instead.

std::vector<C*> vc;
vc.push_back(ne w C);

However if you store raw pointers you will have a memory leak if an
exception gets thrown across the point at which you define vc. Use a
suitable smart pointer instead.


Just a warning for someone who might read this and overlook the word
'suitable', std::auto_ptr does NOT meet the requirements for storage in
the STL containers. Others recommended in this thread may (I haven't
checked).
Jul 23 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9803
by: Hitesh Bhatiya | last post by:
Hi all, I have written a small program to accept some socket connections, which are then added to a vector (using push_back). But after a few calls to the push_back function, it deleted the object that was added last. Could someone please tell me why this happens ? Am I doing something wrong here ?
30
4504
by: Antonios Christofides | last post by:
Hi, As I read in the archives, the performance problem caused by memory reallocations during vector::push_back is a common one. My first C++ program is suffering from it: 300 thousand push_backs result, according to the profiler, in 20 reallocations; these 20 reallocations account for 3.6 seconds (Celeron 1.3G), which is 40% of total execution time. What I don't understand: why is the reallocation code so complex? I
4
1578
by: whocares | last post by:
hi everyone. i'm currently experiencing a strange problem under vc++ 2005 express. i hope someone has a hint for me, i'm kind of lost atm. i'm using a vectors of pointers in my code. using the release build i can add and remove elements aslong as i stay below a certain vector size (13 in this case, no joke). as soon as the vector tries to add element 14 i get a runtime error.
3
3226
by: Al | last post by:
What is the problem with this code? It always crashes at the 'push_back'. I found that thanks to debugging. Any Ideas? Other question: what is a faster way to convert a string to an integer? Is there a built-in function to do that? Here's the code: #include <iostream> #include <fstream> #include <vector> #include <string>
6
21032
by: Siam | last post by:
Hi, I'm a little new to stl so bear with me...Say I have the following code: vector<intvec; int i = 3; vec.push_back(i); i=4; cout<<vec.at(0)<<endl;
6
11617
by: jmsanchezdiaz | last post by:
CPP question: if i had a struct like "struct str { int a; int b };" and a vector "std::vector < str test;" and wanted to push_back a struct, would i have to define the struct, fill it, and then push_back it, or could i pushback the two ints directly somehow? Thanks for all.
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.