D-tor apparentlly isn't called here

P

Protoman

#include <iostream>
#include <cstdlib>
#include <cmath>
#include "SmrtPtr.hpp"
using namespace std;

struct Data
{
double plus;
double mult;
double pow;
};

SmrtPtr<Data> math(const double& val,const double& val2);
int main()
{
for(;;)
{
cout << "Enter two values: " << endl;
double val, val2;
cin >> val >> val2;
SmrtPtr<Data> tuple=math(val,val2);
cout << tuple->plus << endl << tuple->mult << endl << tuple->pow <<
endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}

SmrtPtr<Data> math(const double& val,const double& val2)
{
SmrtPtr<Data> tuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}

Here's the code for the class' d-tor:

~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase; cout << "Deleted." << endl;}
else {delete DataBase; cout << "Out of scope. " << endl;}
}
}
 
R

Robbie Hatley

Protoman said:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "SmrtPtr.hpp"
using namespace std;

struct Data
{
double plus;
double mult;
double pow;
};

SmrtPtr<Data> math(const double& val,const double& val2);
int main()
{
for(;;)
{
cout << "Enter two values: " << endl;
double val, val2;
cin >> val >> val2;
SmrtPtr<Data> tuple=math(val,val2);
cout << tuple->plus << endl << tuple->mult << endl << tuple->pow <<
endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}

SmrtPtr<Data> math(const double& val,const double& val2)
{
SmrtPtr<Data> tuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}

As I read that, it's returning a copy of a SmrtPtr object,
so a temporary is made. Since "tuple" is automatic storage
class, the original "tuple" should be destructed when math()
returns. The temporary copy is then returned to the calling
function.

Are you saying this is NOT what's happening when you run it?
Here's the code for the class' d-tor:

~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase; cout << "Deleted." << endl;}
else {delete DataBase; cout << "Out of scope. " << endl;}
}
}

What does it print when you run it?

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
P

Protoman

Robbie said:
Protoman said:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "SmrtPtr.hpp"
using namespace std;

struct Data
{
double plus;
double mult;
double pow;
};

SmrtPtr<Data> math(const double& val,const double& val2);
int main()
{
for(;;)
{
cout << "Enter two values: " << endl;
double val, val2;
cin >> val >> val2;
SmrtPtr<Data> tuple=math(val,val2);
cout << tuple->plus << endl << tuple->mult << endl << tuple->pow <<
endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}

SmrtPtr<Data> math(const double& val,const double& val2)
{
SmrtPtr<Data> tuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}

As I read that, it's returning a copy of a SmrtPtr object,
so a temporary is made. Since "tuple" is automatic storage
class, the original "tuple" should be destructed when math()
returns. The temporary copy is then returned to the calling
function.

Are you saying this is NOT what's happening when you run it?
Here's the code for the class' d-tor:

~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase; cout << "Deleted." << endl;}
else {delete DataBase; cout << "Out of scope. " << endl;}
}
}

What does it print when you run it?

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/

OK, here's the output:

Enter two values:
2 2
4
4
4
Deleted.
Press any key to continue...

Somewhere, it should be printing "Out of scope."
 
R

Robbie Hatley

Protoman said:
OK, here's the output:

Enter two values:
2 2
4
4
4
Deleted.
Press any key to continue...

Somewhere, it should be printing "Out of scope."

The fact that the d'tor printed ANYTHING shows that the d'tor
destructed one SmrtPtr object. Which one, I don't know for
sure, but I'm guessing it was tuple in math(). If you're
expecting it to tell you when it gets through destructing
all the SmrtPtr objects, then OK, but you're going to have
to get rid of that "system("PAUSE");", because the final
d'tor calls will occur only AFTER you return from main.
So if you're launching this from an icon, you'll never see
that message, because it will only flash on your screen for
a nanosecond!

So get rid of the "system("PAUSE");", launch a DOS window,
and launch the program from within the DOS window by typing
it's name. THEN you will see the final d'tor calls.


--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
P

Protoman

Robbie said:
Protoman said:
OK, here's the output:

Enter two values:
2 2
4
4
4
Deleted.
Press any key to continue...

Somewhere, it should be printing "Out of scope."

The fact that the d'tor printed ANYTHING shows that the d'tor
destructed one SmrtPtr object. Which one, I don't know for
sure, but I'm guessing it was tuple in math(). If you're
expecting it to tell you when it gets through destructing
all the SmrtPtr objects, then OK, but you're going to have
to get rid of that "system("PAUSE");", because the final
d'tor calls will occur only AFTER you return from main.
So if you're launching this from an icon, you'll never see
that message, because it will only flash on your screen for
a nanosecond!

So get rid of the "system("PAUSE");", launch a DOS window,
and launch the program from within the DOS window by typing
it's name. THEN you will see the final d'tor calls.


--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/'

I'll try that.
 
B

Bo Persson

"Protoman" <[email protected]> skrev i meddelandet

snip
SmrtPtr<Data> tuple=math(val,val2);
snip


SmrtPtr<Data> math(const double& val,const double& val2)
{
SmrtPtr<Data> tuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}

The short answer is: It might, but it doesn't have to.


This is an example of the Named Return Value Optimization (NRVO)
specifically allowed by the language standard. The math function
creates a local variable, that is only used to initialize an outer
variable. In that case, the compiler is allowed to skip the local
variable and construct the result directly into the outer variable.


Bo Persson
 
P

Protoman

Bo said:
"Protoman" <[email protected]> skrev i meddelandet

snip


The short answer is: It might, but it doesn't have to.


This is an example of the Named Return Value Optimization (NRVO)
specifically allowed by the language standard. The math function
creates a local variable, that is only used to initialize an outer
variable. In that case, the compiler is allowed to skip the local
variable and construct the result directly into the outer variable.


Bo Persson

I think Dev-Cpp is doing that, then. But wait, how can it skip the
construction of tuple; it's members are being assigned to. If it skips
th construction of tuple, what is being assigned to then?
 
B

Bo Persson

Protoman said:
I think Dev-Cpp is doing that, then. But wait, how can it skip the
construction of tuple; it's members are being assigned to. If it
skips
th construction of tuple, what is being assigned to then?

It is being assigned to the outer tuple variable (in main), where the
result will end up anyway. You can think of it as-if it was written
(almost) like this:

void math(const double& val,const double& val2, SmrtPtr<Data>& tuple)
{
tuple = new Data;
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return;
// no d-tor gets called here!
}


Bo Persson
 
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Protoman wrote:

[snip]
I think Dev-Cpp is doing that, then. But wait, how can it skip the
construction of tuple; it's members are being assigned to. If it skips
th construction of tuple, what is being assigned to then?

Of course it will still have to construct the object. What Bo Persson
meant was that, instead of creating a local object and returning a copy
of it to the caller, an implementation is allowed to provide an
optimization where a single copy of the object is created, where the
outgoing variable would be. This way, the object manipulated within the
function and that which will be returned to the calling code are the
same, not copies.

Regards,
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top