std::vector and object copying

B

bb

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

Jonathan Mcdougall

bb said:
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
 
S

samy.rolka

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

Tomás

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
 
J

Jonathan Mcdougall

Tomás said:
bb posted:


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
 
P

Pete Becker

Jonathan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top