Thread: I don't understand...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Hangzhou,China
    Posts
    7

    Smile I don't understand...

    a simple example destructor,i really donot understand the output
    Code:
    #include<iostream>
    using namespace std;
    
    class T {
    public:
    	T( double d ) : pd( new double( d ) ) { };
    	~T();
    private:
    	double *pd;
    };
    T::~T()
    {
    	cout << ( *pd ) << endl;
    //	( *pd ) = -1;
    //	delete pd;
    }
    
    extern void func( T t )
    {
    	/* ... */
    	cout << " Here " << endl;
    }
    
    void main()
    {
    	T t( 1.2 );
    	T t1( 1.8 );
    	func( t );
    	func( t1 );
    }
    the output is like this:
    Here
    1.2
    Here
    1.8
    1.8
    1.2

    but in my opinion,it should be
    Here
    1.8
    Here
    1.2

    so why? Could anyone explain it to me?
    any help is appreciated...

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    It's right, here's the breakdown. btw this is a good example of why it's inefficient to pass by value. pass by reference would remove the first 1.8 and 1st 1.2, making the output
    Here
    Here
    1.8
    1.2

    t constructed <- no output
    t1 constructed <- no output
    func(t) called <- copy of t made <- no output
    Here <- function body outputs here
    1.2 <- function ends, copy of t destructed, outputs 1.2
    func(t1) called <- copy of t1 made <- no output
    Here <- function body outputs here
    1.8 <- function ends, copy of t1 destructed, outputs 1.8
    1.8 <- main ends variables destructed in reverse order of creation
    1.2 <- first t1, then t
    Last edited by Darryl; 06-25-2005 at 01:20 PM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Hangzhou,China
    Posts
    7
    got it!
    thx very much

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That code ought to crash, by the way, and it would if you hadn't commented out the two lines in the destructor. As it is, it has a memory leak.

    It's a good example why classes with dynamic memory always need a copy constructor.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CornedBee
    That code ought to crash, by the way, and it would if you hadn't commented out the two lines in the destructor. As it is, it has a memory leak.

    It's a good example why classes with dynamic memory always need a copy constructor.
    You're correct, but I doubt the reasons would be obvious to beginners. You may wish to expand on your comments......

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I might, but I think it would be easier (for me) if the poster just searched for the phrase "deleted twice".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    an advice. Whenever you delete a pointer set it to null. You can use some templated function to do that automaticly for you always.
    Code:
    template<class Xptr> inline delete_ptr(Xptr*& p){
        delete p;
        p = NULL;
    }
    template<class Xptr> inline delete_array(Xptr*& p){
        delete[] p;
        p = NULL;
    }

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Wouldn't have helped in this case. It's two different pointers pointing to the same location he's deleting, so setting each to NULL after deleting them wouldn't change a thing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    So I've seen now.
    Lorin, you have to define a copy constructor, and by the way the operator=(...)

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    >> Lorin, you have to define a copy constructor, and by the way the operator=(...)
    or make them both private

    or use smart pointers
    signature under construction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 10-14-2006, 04:38 AM
  2. Need software to help to understand C source code
    By Kincider in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 09:44 PM
  3. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  4. Need help to understand x["AC"] in the following code
    By jcourcel in forum C Programming
    Replies: 11
    Last Post: 06-06-2006, 01:13 AM
  5. I don't understand K&R example
    By rllovera in forum C Programming
    Replies: 8
    Last Post: 10-25-2004, 10:45 AM