CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Destructor

  1. #1
    Join Date
    Dec 2006
    Posts
    7

    Question Destructor

    I have got some doubt on destructor call.
    When the object is deleted then the destructor is called.

    When the object is deleted?
    stack object when goest out of scope and heap object when we call a delete.

    Please find the below code where object is allocated using new.

    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    
    struct st{
            st(int v):val(v){cout<<"st constructed  for "<<val<<endl;}
            ~st(){cout<<"st desctructed  for "<<val<<endl;}
            int val;
    };
    
    
    int main()
    {
            {
            st *p = new st(1000);
            st dd = *(p);
            }
    
            cout<<"Before Program ends"<<endl;
    }
    Here i am not calling delete but assigned to a stack object and when i run the program i see the output as

    st constructed for 1000
    st desctructed for 1000
    Before Program ends

    so is the memory got de-allocated? Though i do not call delete.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Destructor

    It might be helpful if you printed the addresses of the objects as well, and implemented the copy constructor see it being invoked:
    Code:
    #include<iostream>
    
    using namespace std;
    
    struct st {
        st(int v) : val(v) {
            cout << "st constructed @ " << this << " for " << val << endl;
        }
        st(const st& other) : val(other.val) {
            cout << "st copy constructed @ " << this << " from " << &other << " for " << val << endl;
        }
        ~st() {
            cout << "st @ " << this << " destroyed for " << val << endl;
        }
        int val;
    };
    
    int main()
    {
        {
            st *p = new st(1000);
            st dd = *(p);
        }
    
        cout << "Before Program ends" << endl;
    }
    Now I get:
    Code:
    st constructed @ 0x1486c20 for 1000
    st copy constructed @ 0x7ffc5f0f4ae0 from 0x1486c20 for 1000
    st @ 0x7ffc5f0f4ae0 destroyed for 1000
    Before Program ends
    so you can see that you constructed two st objects, one at 0x1486c20 and the other at 0x7ffc5f0f4ae0, but only the object at 0x7ffc5f0f4ae0 had the destructor invoked for it.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Dec 2006
    Posts
    7

    Re: Destructor

    Thanks a lot. It is clear now

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: Destructor

    Quote Originally Posted by raj1986 View Post
    When the object is deleted?
    stack object when goest out of scope and heap object when we call a delete.
    There's a third option. As of C++ 11 a so called (reference counting) smart pointer became part of the standard. It's called std::shared_ptr.

    This smart pointer is used like an ordinary pointer but is "smart" in the sense that it knows when the object it points to on the heap may be safely deleted. This happens when all copies of the smart pointer has gone out of scope so the heap object is no longer referenced from anywhere. Then the smart pointer deletes the object.
    Last edited by wolle; November 20th, 2018 at 03:21 AM.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Destructor

    Quote Originally Posted by wolle View Post
    There's a third option. As of C++ 11 a so called (reference counting) smart pointer became part of the standard. It's called std::shared_ptr.

    This smart pointer is used like an ordinary pointer but is "smart" in the sense that it knows when the object it points to on the heap may be safely deleted. This happens when all copies of the smart pointer has gone out of scope so the heap object is no longer referenced from anywhere. Then the smart pointer deletes the object.
    Or if the ownership of the memory doesn't need to be shared, then std::unique_ptr would be used. See http://www.cplusplus.com/reference/memory/unique_ptr/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured