Thread: Beginner question about destructors!

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    Beginner question about destructors!

    Hi,

    I am wondering why does the below code not behave as i would expect:

    Code:
    #include <iostream>
    using namespace std;
    
    struct error{};
    
    class Trace{
      public:
        Trace(const char *string_pt = "done") ;
        ~Trace();
      private:
       const char *string;
    };
    
    // declare constructor
    
    Trace::Trace (const char *string_pt) 
    	: string(string_pt)
    {
      cout << "+ " << string << endl;
    }
    
    // declare destructor
    Trace::~Trace(){
      cout << "- " << string << endl;
    }
    
    int main(){
      Trace string("A1");
      Trace string1("A2");
      
      for(int i = 0; i<3;i++){
        Trace tp("inLoop");
      }
      Trace *tpp =0;
      {
        Trace string("B1");
        Trace *string1 = new Trace("B2");
        tpp = new Trace("Here I am!");
      }
      
      delete tpp;
      return 0;
    
    }
    During destruction it destructs everything except string1. This i claim based upon the output:

    Code:
    + A1
    + A2
    + inLoop
    - inLoop
    + inLoop
    - inLoop
    + inLoop
    - inLoop
    + B1
    + B2
    + Here I am!
    - B1
    - Here I am!
    - A2
    - A1
    What happened to B2 ???

    Thank you

    baxy

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    C++ isn't a garbage collected language, so make sure to 'delete' your garbage, and not cause memory leaks.
    Code:
        Trace string("B1");
     //this is created on the stack and destructed on scope exit.
    
        Trace *string1 = new Trace("B2");
     //this is allocated on the heap and exists until you explicitly call delete.
    Btw, write the same code using a smart pointer (as oppossed to the normal pointer here) in the inner loop, and you'll see it destructed when not required anymore.
    Last edited by manasij7479; 06-02-2012 at 07:22 AM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    #include <memory>
    auto string = std::make_shared<Trace>("B2");
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I don't see std::make_shared in the standard library documentation. can you point to a reference?

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    make_shared was not in the 1998 C++ standard. IIRC (I haven't checked relevant documentation) it was introduced as part of boost, and is now a feature of C++-11.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about destructors
    By Xanderbeard in forum C++ Programming
    Replies: 6
    Last Post: 05-24-2010, 05:00 PM
  2. Question about Constructors and Destructors with Pointers
    By specter in forum C++ Programming
    Replies: 14
    Last Post: 04-01-2010, 09:39 AM
  3. very beginner's question
    By convenientstore in forum C Programming
    Replies: 6
    Last Post: 02-23-2009, 11:23 AM
  4. A very beginner question!
    By sifeet in forum C Programming
    Replies: 8
    Last Post: 11-06-2007, 07:13 AM
  5. Replies: 2
    Last Post: 06-11-2006, 05:56 PM