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

Why is the copy ctor called twice here?

The output of this:

#include <iostream>
#include <vector>
using namespace std;

struct X {
int i;
X(const X& x) : i(x.i) {
cout << "ctor copy: " << i << endl;
}
X(int ii) : i(ii) {
cout << "ctor by int: " << i << endl;
}
~X() {
cout << "dtor: " << i << endl;
}
};

int main(int argc, char **argv) {

vector<X> v;
v.push_back(X(100));

return 0;
}

Is this:

ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100

When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?

Jul 23 '05 #1
10 1332
ri***********@yahoo.co.uk wrote:

The output of this:

#include <iostream>
#include <vector>
using namespace std;

struct X {
int i;
X(const X& x) : i(x.i) {
cout << "ctor copy: " << i << endl;
}
X(int ii) : i(ii) {
cout << "ctor by int: " << i << endl;
}
~X() {
cout << "dtor: " << i << endl;
}
};

int main(int argc, char **argv) {

vector<X> v;
v.push_back(X(100));

return 0;
}

Is this:

ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100

When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?


From the language point of view, there is no specific reason. Ask in a newsgroup
dedicated to your particular compiler. It could be an error in its vector-implementation.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
ri***********@yahoo.co.uk wrote:
The output of this:
<snip program>
Is this:

ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100

When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?


On my compiler (g++ 3.3), I get what you would expect, namely:

ctor by int: 100
ctor copy: 100
dtor: 100
dtor: 100

I am unsure why your vector is behaving differently. It is possible
push_back takes it's input by value rather than by reference (which
would be wrong), or that the vector is deciding to extend earlier than
is necessary (which would also be wrong), or something else. You could
if you wish explore this by looking at the source to vector, which
should be included in your compiler (as very few compilers can compile
templates seperatly and therefore include the source in the headers).

Unfortunatly I suspect it's unlikely such a bug would be fixed so you
may have to upgrade your compiler (although you can always report it)

Chris
Jul 23 '05 #3
"Chris Jefferson" <ca*@cs.york.ac.uk> wrote in message
news:cu**********@pump1.york.ac.uk...
ri***********@yahoo.co.uk wrote:
The output of this:
<snip program>
Is this:

ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100

When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?


On my compiler (g++ 3.3), I get what you would expect, namely:

ctor by int: 100
ctor copy: 100
dtor: 100
dtor: 100


Odd! On my compiler (g++ 3.4.2) I get
ctor by int: 100
ctor copy: 100
dtor: 100

Something is afoot!
--
Gary
Jul 23 '05 #4
ri***********@yahoo.co.uk wrote:
...
int main(int argc, char **argv) {

vector<X> v;
v.push_back(X(100));

return 0;
}

Is this:

ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100

When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?


It is possible that the first copy is made when the compiler initializes
the [constant reference] parameter of 'push_pack' with a temporary
object 'X(100)'. 'X(100)' is not an lvalue, which means that in
accordance with 8.5.2/5 the compiler is allowed to create an extra copy
of the object when initializing that reference (actually, in this case
the compiler is allowed create as many extra copies as it wants).

The second copy constructor call takes place when the parameter of
'push_pack' is transferred to the actual vector memory.

Apparently, some internal quirk of the compiler caused it to create that
unnecessary first copy (although there's nothing illegal in it). AFAIK,
most compilers won't do it.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #5
Why would push_back taking input by value be wrong?

Regards
Senthil

Jul 23 '05 #6
"sadhu" <se**********@wdevs.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Why would push_back taking input by value be wrong?


Because the C++ standard defines its parameter
as a const reference.

=============================
ISO/IEC 14882:1998(E)

23.2.4 Template class vector

// 23.2.4.3 modifiers:
void push_back(const T& x);
=============================
-Mike
Jul 23 '05 #7
I think this is shabby re-allocation on the MS compiler (it is MS we're
talking about here right?)

if you provide a default constructor for X, and replace your main with:
int main(int argc, char** argv)
{
const X & x1(100);

vector<X> v;

v.reserve(1);

std::cout << "\n\n" << std::endl;

v.push_back(x1);

return 0;

}

You can get a clearer idea of where the copies are taking place - explicitly
instantiating as a reference removes any copy it may have caused when
created via the push_back parameter - and yet if you remove the reserve(1)
the same behaviour you are seeing repeats - which means in my book it's just
bad re-allocation when inserting in the vector.
regards,

Aiden.

<ri***********@yahoo.co.uk> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
The output of this:

#include <iostream>
#include <vector>
using namespace std;

struct X {
int i;
X(const X& x) : i(x.i) {
cout << "ctor copy: " << i << endl;
}
X(int ii) : i(ii) {
cout << "ctor by int: " << i << endl;
}
~X() {
cout << "dtor: " << i << endl;
}
};

int main(int argc, char **argv) {

vector<X> v;
v.push_back(X(100));

return 0;
}

Is this:

ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100

When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #8
adbarnet wrote:

if you provide a default constructor for X, and replace your main with:
Why do you think 'X' needs a default constructor? In the code below I
don't see any place where it might be necessary.
int main(int argc, char** argv)
{
const X & x1(100);

vector<X> v;

v.reserve(1);

std::cout << "\n\n" << std::endl;

v.push_back(x1);

return 0;

}


--
Best regards,
Andrey Tarasevich
Jul 23 '05 #9
Quite right - it's not required.

regards,

Aiden

"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:11*************@news.supernews.com...
adbarnet wrote:

if you provide a default constructor for X, and replace your main with:


Why do you think 'X' needs a default constructor? In the code below I
don't see any place where it might be necessary.
int main(int argc, char** argv)
{
const X & x1(100);

vector<X> v;

v.reserve(1);

std::cout << "\n\n" << std::endl;

v.push_back(x1);

return 0;

}


--
Best regards,
Andrey Tarasevich


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #10

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:VZ********************@comcast.com...
"Chris Jefferson" <ca*@cs.york.ac.uk> wrote in message
news:cu**********@pump1.york.ac.uk...
ri***********@yahoo.co.uk wrote:
The output of this:
<snip program>
Is this:

ctor by int: 100
ctor copy: 100
ctor copy: 100
dtor: 100
dtor: 100
dtor: 100

When you do the push_back, I can understand the vector wanting to make
its own copy, but why is this apparently done twice?


On my compiler (g++ 3.3), I get what you would expect, namely:

ctor by int: 100
ctor copy: 100
dtor: 100
dtor: 100


Odd! On my compiler (g++ 3.4.2) I get
ctor by int: 100
ctor copy: 100
dtor: 100

Something is afoot!
--
Gary

I noticed similar behavior when I ran this example in VC++, I was a little
curious why the destructor calls
didn't match the constructor calls. I had a breakpoint on the main return
statement, and only saw the one destructor call. However, if you continue
after the return, the second
destructor is triggered as we leave the main() scope. The behavior you
reported above is certainly incorrect
otherwise.

dave


Oct 2 '05 #11

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

Similar topics

39
by: scooter | last post by:
Given this class heirarchy class Base{}; class A : public Base {}; class B : public Base {};
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
7
by: skishorev | last post by:
Hi, I know What is the copy constructor. I don't know where and why we have to use copy constructor. If You know please give to me with a situation where we have to use or with a small example....
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
9
by: blangela | last post by:
2.0 Sample Code class ABC // dummy class used below {}; class Example2 { public: Example2(); // default ctor Example2( const Example2 &); // copy ctor
1
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
15
by: subramanian100in | last post by:
consider the following program: #include <iostream> using namespace std; class Test { public: Test(int xx) : x(xx) { cout << x << endl; }
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.