Thread: >> Destructor Question

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    71

    >> Destructor Question

    Hey all,

    Code:
    class Test {
        int m_Val;
    
        public:
    
        void printVal() { cout << m_Val << endl; }
    
        ~Test() { m_Val = 5;}
        Test(int val) { m_Val = val; printVal(); this->~Test(); }
    };
    
    int main()
    {
        Test(5);
    
        Test a(10);
    
        a.printVal();
    
        return 0;
    }
    I was playing around a little, and found out that this not only compiles, but the last call produces the result "5". I though a destructor destroys the class, so the program shouldn't be able to access the variable. Or is this accessing something out of bounds?

    Cheers,

    Gabe

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by G4B3
    I was playing around a little, and found out that this not only compiles, but the last call produces the result "5". I though a destructor destroys the class, so the program shouldn't be able to access the variable. Or is this accessing something out of bounds?
    I uh, have never considered implementing a miscarriage instead of a constructor. Your program has undefined behaviour for two reasons:
    • The explicit destructor call followed by the construction of the object on the stack means that the destructor will be called on a destroyed object (i.e., it will be called a second time, if that even makes sense).
    • The member function printVal() is called on a destroyed object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    I would venture to guess that your explicit call to the destructor causes the destructor to be executed, setting m_Val to 5, but the destruction has little effect until something else overwrites the memory used by the object, which doesn't happen in your small program. Explicitly calling the destructor for an object created on the stack can cause serious problems, because the compiler will call the destructor again when the object goes out of scope.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There is only one case where calling the destructor explicitly is valid: when the instance was created with placement new.

    Code:
    #include <iostream>
    using namespace std;
    
    class Test {
        int m_Val;
    
        public:
    
        void printVal() { cout << m_Val << endl; }
    
        ~Test() { cout << "~Test\n"; }
        Test(int val) { m_Val = val; }
    };
    
    int main()
    {
        char buffer[sizeof(Test)];
    
        //put a Test object into the buffer of bytes
        Test* p_test = new (buffer) Test(42);
        p_test->printVal();
    
        //now call the destructor, which wouldn't be called otherwise
        p_test->~Test();
        return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    Thanks for the responses. I know you're not supposed to do that even when mommy's not watching, as I said I was just playing around.

    Anon: I'm not sure I understand what's going on there...I've never seen new used in such a way. Do you think you could give a more detailed explanation?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by G4B3
    Anon: I'm not sure I understand what's going on there...I've never seen new used in such a way. Do you think you could give a more detailed explanation?
    You might want to search the Web for "placement new".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    Wow, never heard of that before. Thanks laserlight ;-)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In particular, the article from C++ FAQ Lite would be a good starting point. The Wikipedia article also looks trustworthy. Danny Kalev's devx article is too short, but if you search for array placement new instead, his relevant devx article would be a good read.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  2. cin >> buf endless loop
    By cfriend in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2005, 04:01 PM
  3. Overloading << and >>
    By Enahs in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2005, 04:33 PM
  4. overloaded >> operator problem
    By quizkiwi in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2005, 03:27 PM
  5. C++ >> Standard Library >> Header File
    By hrk2006 in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2003, 08:30 PM