473,513 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i've got strange destructor

Hey,

Here's a sample code you can easily guess the result. <result 1>

#include <iostream>
using namespace std;
class AA
{
public:
AA() { cout<<"AA();"<<endl; }
AA(const AA&) { cout<<"AA(const AA&);"<<endl; }
~AA() { cout<<"~AA();"<<endl; }
};

void Foo(AA aa)
{
}

int main(int, char**)
{
Foo(AA());
}

BUT, I found strange result when I removed copy constructor of AA
class. <result 2>

(vs.net 2003 - ms c/c++ compiler 13.10.3077)

<result 1>
AA();
~AA();
<result 2>
AA();
~AA();
~AA();
Can anyone explain me the reason why the destructor called twice when I
removed copy constructor ?

Sep 26 '05 #1
4 1285
al******@gmail.com wrote:
Hey,

Here's a sample code you can easily guess the result. <result 1>

#include <iostream>
using namespace std;
class AA
{
public:
AA() { cout<<"AA();"<<endl; }
AA(const AA&) { cout<<"AA(const AA&);"<<endl; }
~AA() { cout<<"~AA();"<<endl; }
};

void Foo(AA aa)
{
}

int main(int, char**)
{
Foo(AA());
}

BUT, I found strange result when I removed copy constructor of AA
class. <result 2>

(vs.net 2003 - ms c/c++ compiler 13.10.3077)

<result 1>
AA();
~AA();
<result 2>
AA();
~AA();
~AA();
Can anyone explain me the reason why the destructor called twice when I
removed copy constructor ?


Your example is odd, but perfectly legal. In your code you create a
temporary AA object with the default constructor and then create the
parameter aa by copy constructing the temporary. In <result 2> you see
this happen, the first destructor call is for the aa parameter the
second for the temporary. So two objects are created and two objects are
destroyed but you don't see the copy constructor call because the
compiler generated copy constructor doesn't print anything.

The compiler is allowed to optimise away the temporary use the default
constructor directly on parameter aa. It is allowed to do this even if
the copy constructor has a side effect (moral is, don't put anything in
a copy constructor that you don't mind being optimised away). So you can
have just one object created and one object destroyed and this is what
you see in <result 1>.

For me the odd part is that you only see this optimisation when you have
defined a copy constructor, you don't see it when you use the compiler
generated copy constructor. I would have thought it would be more likely
the other way around.

So odd, but legal.

john
Sep 26 '05 #2
So if I didn't want the optimisation to take place, and I did want the
call to void Foo(AA aa) to have the copy ctor called for aa, then how
would I do that? Is it simply a case of using aa in Foo or is there
more to that?

G

Sep 26 '05 #3
"Gr*****@nospam.com" wrote:

So if I didn't want the optimisation to take place, and I did want the
call to void Foo(AA aa) to have the copy ctor called for aa, then how
would I do that? Is it simply a case of using aa in Foo or is there
more to that?


There is more to it.
You need to tell your compiler to not do that optimization.
Depending on your compiler this might be possible or it
might not be possible.

Moral of story: Write your copy constructor in a way such that
it doesn't matter if it is optimized away. As John Harris
already said.
--
Karl Heinz Buchegger
kb******@gascad.at
Sep 26 '05 #4
Gr*****@nospam.com wrote:
So if I didn't want the optimisation to take place, and I did want the
call to void Foo(AA aa) to have the copy ctor called for aa, then how
would I do that? Is it simply a case of using aa in Foo or is there
more to that?

G


If you are willing to change your code slightly

void Foo(const AA& aa)
{
...
}

Using a reference means no copy constructor will be called.

john
Sep 26 '05 #5

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

Similar topics

1
3324
by: Hector Villafuerte | last post by:
Hi, I have been using Python 2.3.4 with MySQL 4.0.16-max-nt (through MySQLdb) for months. Today this messages showed up: Exception exceptions.AttributeError: "'NoneType' object has no attribute...
11
10494
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
9
1506
by: Gutek | last post by:
Hello all, I have run into the following problem with virtual destructor. I have following classes: class A { public: double* p; A(unsigned int s=0) : { p = new double ; }
4
5009
by: Abhishek Srivastava | last post by:
Hello All, I wrote a program in which I have a OleDbConnection which I open in the contructor and close in the Destructor. When I run the program I get the following error Unhandled...
4
1431
by: r.z. | last post by:
My program behaves very strange. I keep getting the following error: 'The instruction at "some address" referenced memory at "some address". The memory could not be written"' The program consists...
4
342
by: mike3 | last post by:
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even...
2
2111
by: Spes | last post by:
Hi, I have this simple code: | #!/usr/bin/python | import codecs | import re | from copy import deepcopy | | class MyClass(object): | def __del__(self):
19
1728
by: david | last post by:
I took old code and decided to modify it a bit, and I just noticed that it does not compile at all and before server one of severs (main) crashed in the system it was working fine (I am really sure...
18
5261
by: Eric | last post by:
Ok...this seems to be treading into some really esoteric areas of c++. I will do my best to explain this issue, but I don't fully understand what is happening myself. I am hoping that the problem...
0
7260
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
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
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
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
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
4746
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.