0

As i was going through smart pointers, I ran through the following code.

Works as expected.

#include <iostream>
#include <memory>

using namespace std;

class Double
{
public:
    Double(double d = 0) : dValue(d) { cout << "constructor: " << dValue << endl; } 
    ~Double() { cout << "destructor called with exception: " << dValue << endl; }
    void setDouble(double d) { dValue = d; 
        float temp= d/0;
        cout<<"Error\n";
    }
private:
    double dValue;
}; 

int main()
{
    auto_ptr<Double> ptr(new Double(3.14));
    (*ptr).setDouble(6.28); 

    return 0;
}

As you can see there is an exception in setDouble method i.e i am trying divided by zero, since i am using the auto_ptr here even though there is the exception the allocated object for the class Double will be destroyed and destructor will be called this is as expected. But i have encountered one issue when i changed the code snippet, instead of dividing the d by zero, i use hard code value 1 and divide it by zero in this case also this is an exception, but as i am using auto pointer i was expecting the allocated object to be deleted, for my surprise the exception occurs and object of Double is not destroyed.

Not expected behavior.

#include <iostream>
#include <memory>

using namespace std;

class Double
{
public:
    Double(double d = 0) : dValue(d) { cout << "constructor: " << dValue << endl; } 
    ~Double() { cout << "destructor called with exception: " << dValue << endl; }
    void setDouble(double d) { dValue = d; 
        float temp= 1/0;
        cout<<"Error\n";
    }
private:
    double dValue;
}; 

int main()
{
    auto_ptr<Double> ptr(new Double(3.14));
    (*ptr).setDouble(6.28); 

    return 0;
}

Can anyone help me to understand this, why it is not behaving as expected when i hard code 1/0 in the setDouble method.

Thank you.

Newbie
  • 204
  • 1
  • 3
  • 13
  • 1
    Why are you using `auto_ptr`? There is a reason it's deprecated in C++11 (and even removed afterwards). – Nelfeal Dec 15 '18 at 06:24
  • 1
    Also, integer division by zero is not an exception. I don't see why you think an exception is thrown anywhere in your code. – Nelfeal Dec 15 '18 at 06:33
  • Thank you for the comment , I have tested this with unique_ptr as well and faced the same issue. @Nelfeal – Newbie Dec 15 '18 at 06:34
  • I guess this is nothing to do with pointers , anything division by zero is an exception, the program crashes whenever i do division with hard code values @Nelfeal. – Newbie Dec 15 '18 at 06:42
  • 1
    You are mistaken. An integer division by zero leads to undefined behavior, which may or may not make the program crash on the spot. If your two snippets behave differently for you, that's simply a result of the behavior being *undefined*. Exceptions are something else entirely, completely unrelated, and well-defined (as opposed to undefined). – Nelfeal Dec 15 '18 at 06:45
  • appreciate your info , i completely understand what you are trying to say here. – Newbie Dec 15 '18 at 06:56
  • I have, and both give me the same output without crashing. I'm not going to repeat what I said about undefined behavior. If you don't understand it, go read a book or do some research on it. – Nelfeal Dec 15 '18 at 07:00
  • I got it, thank you.@Nelfeal – Newbie Dec 15 '18 at 07:08
  • `float temp= d/0;` is essentially `float temp = static_cast( d / static_cast(0));` (because of type-promotion rules). Dividing a `double` by `0.0` will on many compilers generate an infinity (unless `d` is 0 as well, but see this question as well: https://stackoverflow.com/questions/42926763). As others already stated: *At no point in your code, exceptions are thrown* – chtz Dec 18 '18 at 12:49

0 Answers0