 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
richardclay09@yahoo.co.uk Guest
|
Posted: Thu Feb 10, 2005 2:22 pm Post subject: 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
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?
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Thu Feb 10, 2005 2:28 pm Post subject: Re: Why is the copy ctor called twice here? |
|
|
[email]richardclay09 (AT) yahoo (DOT) co.uk[/email] wrote:
| Quote: |
The output of this:
#include <iostream
#include
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
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
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
Chris Jefferson Guest
|
Posted: Thu Feb 10, 2005 2:51 pm Post subject: Re: Why is the copy ctor called twice here? |
|
|
[email]richardclay09 (AT) yahoo (DOT) co.uk[/email] wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Gary Labowitz Guest
|
Posted: Thu Feb 10, 2005 5:32 pm Post subject: Re: Why is the copy ctor called twice here? |
|
|
"Chris Jefferson" <caj (AT) cs (DOT) york.ac.uk> wrote
| Quote: | richardclay09 (AT) yahoo (DOT) 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
|
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Thu Feb 10, 2005 6:15 pm Post subject: Re: Why is the copy ctor called twice here? |
|
|
[email]richardclay09 (AT) yahoo (DOT) co.uk[/email] wrote:
| Quote: | ...
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
|
|
| Back to top |
|
 |
sadhu Guest
|
Posted: Thu Feb 10, 2005 8:51 pm Post subject: Re: Why is the copy ctor called twice here? |
|
|
Why would push_back taking input by value be wrong?
Regards
Senthil
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Thu Feb 10, 2005 9:30 pm Post subject: Re: Why is the copy ctor called twice here? |
|
|
"sadhu" <senthilkumar (AT) wdevs (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
adbarnet Guest
|
Posted: Thu Feb 10, 2005 11:12 pm Post subject: Re: Why is the copy ctor called twice here? |
|
|
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 << "nn" << 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.
| Quote: | The output of this:
#include <iostream
#include
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
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
|
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Thu Feb 10, 2005 11:28 pm Post subject: Re: Why is the copy ctor called twice here? |
|
|
adbarnet wrote:
| Quote: |
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.
| Quote: | int main(int argc, char** argv)
{
const X & x1(100);
vector<X> v;
v.reserve(1);
std::cout << "nn" << std::endl;
v.push_back(x1);
return 0;
}
|
--
Best regards,
Andrey Tarasevich
|
|
| Back to top |
|
 |
adbarnet Guest
|
Posted: Fri Feb 11, 2005 7:53 pm Post subject: Re: Why is the copy ctor called twice here? |
|
|
Quite right - it's not required.
regards,
Aiden
"Andrey Tarasevich" <andreytarasevich (AT) hotmail (DOT) com> wrote
| Quote: | 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 << "nn" << 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
|
|
| Back to top |
|
 |
Dave Townsend Guest
|
Posted: Sun Oct 02, 2005 6:33 pm Post subject: Re: Why is the copy ctor called twice here? |
|
|
"Gary Labowitz" <glabowitz (AT) comcast (DOT) net> wrote
| Quote: | "Chris Jefferson" <caj (AT) cs (DOT) york.ac.uk> wrote in message
news:cufs8u$2l7$1 (AT) pump1 (DOT) york.ac.uk...
[email]richardclay09 (AT) yahoo (DOT) co.uk[/email] 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|