473,394 Members | 1,971 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,394 software developers and data experts.

efficiency of vector::push_back?

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 #1
17 4123
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?

Thanks,
Mark


I believe what happens is the:

1. vc is created with 0 capacity.
2. When push_back is called a temporary C object is constructed.
3. vc is then grown to capacity of 1.
4. As the vector is grown, another C object is constructed. This one is
inside of vc
5. Then the temporary C object (created by the call to push_back in 2) is
then copied (via the operator= or copy constructor into the C object that
is inside the vector (created in 4).
Jul 23 '05 #2
>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?


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(new C);

--
Jonathan
[FAQ] - http://www.parashift.com/c++-faq-lite/

Jul 23 '05 #3
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.
Yes, but...
I realize [that] 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? cat main.cc #include <iostream>
#include <vector>

class C {
private:
// representation
int I;
public:
// operators
friend
std::ostream& operator<<(std::ostream& os, const C& c) {
return os << c.I;
}
// constructors
C(int i = 0): I(i) {
std::cerr << "C::C(int)" << std::endl;
}
C(const C& c): I(c.I) {
std::cerr << "C::C(const C&)" << std::endl;
}
~C(void) {
std::cerr << "C::~C(void)" << std::endl;
}
};

int main(int argc, char* argv[]) {
std::vector<C> vc;
vc.push_back(C());
return 0;
}
g++ -Wall -ansi -pedantic -O3 -o main main.cc
./main

C::C(int)
C::C(const C&)
C::~C(void)
C::~C(void)

Since the constructors, destructors and the push_back function
are all defined to be inline functions
the compiler can optimize away everything
except the diagnostic messages to standard error.
Jul 23 '05 #4
E. Robert Tisdale wrote:
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.

Yes, but...
I realize [that] 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?


> cat main.cc

#include <iostream>
#include <vector>

class C {
private:
// representation
int I;
public:
// operators
friend
std::ostream& operator<<(std::ostream& os, const C& c) {
return os << c.I;
}
// constructors
C(int i = 0): I(i) {
std::cerr << "C::C(int)" << std::endl;
}
C(const C& c): I(c.I) {
std::cerr << "C::C(const C&)" << std::endl;
}
~C(void) {
std::cerr << "C::~C(void)" << std::endl;
}
};

int main(int argc, char* argv[]) {
std::vector<C> vc;
vc.push_back(C());
return 0;
}
> g++ -Wall -ansi -pedantic -O3 -o main main.cc
> ./main

C::C(int)
C::C(const C&)
C::~C(void)
C::~C(void)

Since the constructors, destructors and the push_back function
are all defined to be inline functions
the compiler can optimize away everything
except the diagnostic messages to standard error.


I don't understand your conclusions here. It appears that the
temporaary is constructed and then copied rather than being constructed
"in place" within the vector. How do you know that everything but the
std error messages is optimized away?
Jul 23 '05 #5
> 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?


Some compilers might have done that in the past. But it is now forbidden by
the standard (since there are situations where you rely on the exact number
of copy). The only copy they are allowed to optimize away is the return
value of a function. Ex: CObj obj = fun();
--
JS
Jul 23 '05 #6
In article <11*********************@f14g2000cwb.googlegroups. 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(new 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.
--
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 #7
In article <42*********************@news.free.fr>, Jean-Sebastien Samson
<js***@yahoo.fr> writes
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?


Some compilers might have done that in the past. But it is now forbidden by
the standard (since there are situations where you rely on the exact number
of copy). The only copy they are allowed to optimize away is the return
value of a function. Ex: CObj obj = fun();


No. While there is rather more restriction on when a copy may be
optimised away it is nowhere near as strict as that.
--
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 #8
On Fri, 13 May 2005 07:47:51 GMT, Mark P <no*@my.real.email> wrote:
E. Robert Tisdale wrote:
Mark P wrote:
Say I have objects of class C which are fairly large.
Then consider:

vector<C> vc;
vc.push_back(C());
<snip> Since the constructors, destructors and the push_back function
are all defined to be inline functions
the compiler can optimize away everything
except the diagnostic messages to standard error.


I don't understand your conclusions here. It appears that the
temporaary is constructed and then copied rather than being constructed
"in place" within the vector. How do you know that everything but the
std error messages is optimized away?


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".
--

Best wishes,

Bob
Jul 23 '05 #9
In article <j0********************************@4ax.com>, Robert W Hand
<rw****@NOSPAMoperamail.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)

--
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 #10
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.com>,
Mark P <no*@my.real.email> 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=(const 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_ptr 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::shared_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*****@robinton.demon.co.uk> wrote:
In article <j0********************************@4ax.com>, Robert W Hand
<rw****@NOSPAMoperamail.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****@NOSPAMoperamail.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*********************@f14g2000cwb.googlegroups. 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(new 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
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...
30
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...
4
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...
3
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...
6
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
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...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.