f



std::vector and object copying

Hi,

....
std::vector<MyClass> v1;
v1.push_back(MyClass("abc"));
....

The above results in a single call to the 'copy constructor' of MyClass
when I use gcc. However, the same code results in 2 calls to the 'copy
constructor' when I use Visual C++ (VStudio 2005).

VStudio 2005 is the latest from Microsoft and it can't be that bad!. Am
I missing some compile time optimization option? Apologies, if you
think it is a wrong group to bother with.

Thanks.

0
5/9/2006 7:23:42 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

5 Replies
334 Views

Similar Articles

[PageSpeed] 56

bb wrote:
> Hi,
>
> ...
> std::vector<MyClass> v1;
> v1.push_back(MyClass("abc"));
> ...
>
> The above results in a single call to the 'copy constructor' of MyClass
> when I use gcc. However, the same code results in 2 calls to the 'copy
> constructor' when I use Visual C++ (VStudio 2005).

How many times objects are copied is in general implementation-defined.
>From there, to know about a specific compiler's behavior, ask in a
newsgroup supporting that compiler (or to the makers themselves).

It may be that Visual C++'s standard library internally copies the
object before adding it to the container. Use your debugger and have a
look, you can usually step into the standard library's code.


Jonathan

0
5/9/2006 7:39:43 AM
Hi,

VStudio uses un temporary object, as you can see in vector :

....
_Ty _Tmp = _Val;	// in case _Val is in sequence
....

Then, it fill the vector with this tempo... => 2 copies...

0
5/9/2006 8:00:28 AM
bb posted:

> Hi,
> 
> ...
> std::vector<MyClass> v1;
> v1.push_back(MyClass("abc"));
> ...
> 
> The above results in a single call to the 'copy constructor' of MyClass
> when I use gcc. However, the same code results in 2 calls to the 'copy
> constructor' when I use Visual C++ (VStudio 2005).

There's an optimization going on there. You can see where the second one is 
coming from though:

std::vector<MyClass> v1;

MyClass object("abc");

v1.push_back(object); /* <- Results in copy-construction */


Here's some sample code that can create anywhere from 1 to 3 objects (or 
maybe even 4?). First though, here's a handy class for testing:

class AnyClass {
public:
    
    static unsigned times_constructor_called;
    static unsigned times_copy_constructor_called;
    static unsigned times_destructor_called;
    static unsigned times_assignment_performed;
    
    AnyClass()
    {
        ++times_constructor_called;
    }

    AnyClass( const AnyClass & )
    {
        ++times_copy_constructor_called;
    }

    AnyClass& operator=( const AnyClass & )
    {
        ++times_assignment_performed;

        return *this;
    }

    ~AnyClass()
    {
        ++times_destructor_called;
    }
};

unsigned AnyClass::times_constructor_called = 0;
unsigned AnyClass::times_copy_constructor_called = 0;
unsigned AnyClass::times_destructor_called = 0;
unsigned AnyClass::times_assignment_performed = 0;


Now here's some code to try it out on:


AnyClass FuncReturnByValue()
{
    AnyClass local_object;

    return local_object;
}


int main()
{
    AnyClass any_class = FuncReturnByValue();
}


-Tom�s
0
null25 (1555)
5/9/2006 8:14:03 AM
Tom=E1s wrote:
> bb posted:
>
> > Hi,
> >
> > ...
> > std::vector<MyClass> v1;
> > v1.push_back(MyClass("abc"));
> > ...
> >
> > The above results in a single call to the 'copy constructor' of MyClass
> > when I use gcc. However, the same code results in 2 calls to the 'copy
> > constructor' when I use Visual C++ (VStudio 2005).
>
> There's an optimization going on there. You can see where the second one =
is
> coming from though:
>
> std::vector<MyClass> v1;
>
> MyClass object("abc");
>
> v1.push_back(object); /* <- Results in copy-construction */

If you mean std::vector::push_back() takes its argument by value, you
are wrong. It takes a const reference.


Jonathan

0
5/9/2006 8:19:30 AM
Jonathan Mcdougall wrote:
> 
> How many times objects are copied is in general implementation-defined.
> 

Implementation-specific, but not implementation-defined. 
Implementation-defined means that a standard-conforming compiler must 
document what it does.

-- 

Pete Becker
Roundhouse Consulting, Ltd.
0
petebecker (1324)
5/9/2006 10:53:55 AM
Reply: