Beginner question about destructors!

This is a discussion on Beginner question about destructors! within the C++ Programming forums, part of the General Programming Boards category; Hi, I am wondering why does the below code not behave as i would expect: Code: #include <iostream> using namespace ...

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

    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,526
    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 08:22 AM.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    #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
    2,588
    I don't see std::make_shared in the standard library documentation. can you point to a reference?

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,526
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,629
    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, Sunshine, 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, 06:00 PM
  2. Question about Constructors and Destructors with Pointers
    By specter in forum C++ Programming
    Replies: 14
    Last Post: 04-01-2010, 10: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, 06:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21