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

std::vector and object copying

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.

May 10 '06 #1
5 2141
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

May 10 '06 #2
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...

May 10 '06 #3
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
May 10 '06 #4
Tomás 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

May 10 '06 #5
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.
May 10 '06 #6

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

Similar topics

4
by: bartek d | last post by:
Hello, I have a class which is used to encapsulate a RenderMan Interface variable. Generally speaking, such variable may be of integral, float, string type, or an array of those. I thought I...
27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
11
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse...
6
by: isaacyho | last post by:
Is there a fast way to crop a vector? I have a vector of size n, and want to reduce it to some subregion (x,y ). Seems like you shouldn't have to recopy all of those elements into a new vector,...
24
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
4
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this...
3
by: DevNull | last post by:
I have a program where we load a mapfile, comprised of a .csv with numbers which represent object types at a given position. Each position is in the map is called a Cell, a cell contains only a...
5
by: ciccio | last post by:
Hi, Again I am wondering about something. If you use the swap function of an std::vector, what does it do? Does it swap each element separately by means of copying to a tmp variable, or...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.