2

In the code below there is 3 object of d1 and d2 gets created for doing push_back(). One when I have created, one in calling v.push_back() and another when it is actually copied into the vector {of which I am not sure}.

What will be best alternative to avoid this without using std::vector in C++03?

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct Details
{
    string fname;
    string lname;
    string address;
    int age;
};

int main(int argc, char **argv)
{
    vector<Details> v;

    Details d1;
    d1.fname = "vivek";
    d1.lname = "kumar";
    d1.address = "New Delhi";
    d1.age = 25;

    v.push_back(d1);

    Details d2;
    d2.fname = "some name";
    d2.lname = "some lastname";
    d2.address = "some address";
    d2.age = 25;

    v.push_back(d2);

    return 0;
}
7
  • My project still uses C++03 standard. Mar 14, 2013 at 14:14
  • 2
    Don't try to optimize what you haven't evaluated to be a problem. You might end up with needlessly complicated code that is a problem.
    – DevSolar
    Mar 14, 2013 at 14:19
  • Confused. You want to put the same object into a vector twice? Mar 14, 2013 at 14:19
  • 2
    One easy way to get better performance on vector inserts would be to call vector::reserve with the estimated vector size before the push_back operations. Mar 14, 2013 at 14:27
  • 4
    Have to agree with everybody here. First, there is only one copy here, and that's part of what vector does. Second, if you don't want that, you must store raw pointers or some very light pointer wrapper in your vector, but that's not an speed optimization; it will hurt performance because of all the extra cache-misses. Third, Unless you are doing something time-critical and you have actually found this to be your bottleneck, it's not worth optimizing. Fourth, the compiler is likely to optimize some of the extra copying of data out for you; although not all of it.
    – yzt
    Mar 14, 2013 at 14:41

2 Answers 2

6

Calling std::vector::push_back() does not create a copy per se, because a const reference is passed. It only creates the copy that becomes the vector element. And this copy is required by the semantics of std::vector.

You could also push_back() the Details at a time when copying is relatively cheap:

Details d1; // Now the 3 strings of d1 are still small
v.push_back(d1); // essentially copies 3 empty strings and an int.
v.back().fname = "vivek";
v.back().lname = "kumar";
v.back().address = "New Delhi";
v.back().age = 25;
6
  • If you do not want it to copy the data use vector<Details*>.
    – andre
    Mar 14, 2013 at 14:26
  • @andre any combination with smart pointer to avoid dynamic resource management Mar 14, 2013 at 14:29
  • well, smartptr is also an object, so it will be copied (+ ref count updated) Mar 14, 2013 at 14:30
  • honestly in this case just follow the KISS principle. You may not be aware but the compiler can remove temporary copies for you when it optimizes. So you can be trying to re-invent the wheel with your optimization.
    – andre
    Mar 14, 2013 at 14:33
  • I liked this answer better before you added the second paragraph. That 'solution' is harder to read, more painful to manage and you do not provide conclusive evidence that it would offer considerably better performance - we're talking about names and addresses here (strings of length 20 or so), not multiple kbyte-buffers.
    – us2012
    Mar 14, 2013 at 14:51
1

If you really have determined that performance is of paramount importance here then you are going to need/implement reference semantics.

  • In C++11 you have move semantics, so you would implement a move constructor for the Details structure, so I would normally strongly suggest using that
  • Since you are using C++03, you can use boost::shared_ptr to wrap your Details structure in, like this: vector<boost::shared_ptr<Details> >. Since you are using C++03 please mind the space character between >.
  • normally I would recommend unique_ptr instead of shared_ptr, but unique_ptr is only available in C++11, where the move semantics alternative is better most of the time.
  • the reason I'm not recommending raw pointers is because you need to manually manage memory for them, which is a no-no.

As for your example, please note that in C++03 push_back takes a const reference, so there's only one copy of your structure involved. Using any of the methods I suggested above will still do one copy, but the size of the copy will be kept in check and won't scale with the size of your structure, which might be exactly what you want when you are working with large data.

2
  • Can I use vector<tr1::shared_ptr<Details> > ? Mar 14, 2013 at 14:40
  • You did not mention the compiler you are using. From your comment I am assuming VC90 or lower. But yes, tr1::shared_ptr should be fine if it is available to you.
    – Ed Barbu
    Mar 14, 2013 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.