smart pointer and overloaded delete operator

smart pointer and overloaded delete operator

Post by monk » Sun, 27 Jul 2003 21:53:12



hi ,

i am implementing a smart pointer :

template <typename T> class smartpointer{
protected:
  T* m_pT;
public:
              smartpointer           (T* pT=0):m_pT(pT) {}
              ~smartpointer          ()
{delete(m_pT);m_pT=0;}
         T*   operator ->            ()                 {return(m_pT);}
         T&   operator *             ()
{return(*m_pT);}
              operator smartpointer* ()                 {return(this);}
  static void operator delete        (void* pointer)    {}

Quote:};

and i want to know what really happens when the next code is executed

  smartpointer<int> sp_int(new int);
  delete sp_int;

I know there is a call for cast operator smartpointer* and then a call
to the overloaded delete operator, but is there any memory liberation
behind the scenes ?

thank you.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

smart pointer and overloaded delete operator

Post by Dhru » Mon, 28 Jul 2003 19:47:37


 > hi ,
 >
 > i am implementing a smart pointer :
 >
 > template <typename T> class smartpointer{
 > protected:
 >   T* m_pT;
 > public:
 >               smartpointer           (T* pT=0):m_pT(pT) {}
 >               ~smartpointer          ()
 > {delete(m_pT);m_pT=0;}
 >          T*   operator ->            ()                 {return(m_pT);}
 >          T&   operator *             ()
 > {return(*m_pT);}

This is dicy. Are you sure you want to convert.
 >   operator smartpointer* ()       {return(this);}   ______[1]

Usually, smart pointers are supposed to clean up after themselves.
 >   static void operator delete     (void* pointer) {}  ______[2]
 > };
 >
 > and i want to know what really happens when the next code is executed
 >
 >   smartpointer<int> sp_int(new int);
 >   delete sp_int;

This line will not compile, because there is no way you can convert a
smartpointer to a void*.

 >
 > I know there is a call for cast operator smartpointer* and then a call
 > to the overloaded delete operator, but is there any memory liberation
 > behind the scenes ?

No, and
No, there is no memory released what so ever. If you defing an operator
delete for your class, you sould define op. new too.

Regards,
-Dhruv.

ps. for a smart pointer implementation, look at auto_ptr's implementation,
(usually found in the standard header <memory>).

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

smart pointer and overloaded delete operator

Post by Sebastian Molesk » Mon, 28 Jul 2003 19:54:56



 > hi ,
 >
 > i am implementing a smart pointer :
 >
 > template <typename T> class smartpointer{
 > protected:
 >   T* m_pT;
 > public:
 >               smartpointer           (T* pT=0):m_pT(pT) {}
 >               ~smartpointer          ()
 > {delete(m_pT);m_pT=0;}
 >          T*   operator ->            ()                 {return(m_pT);}
 >          T&   operator *             ()
 > {return(*m_pT);}
 >               operator smartpointer* ()                 {return(this);}
 >   static void operator delete        (void* pointer)    {}
 > };
 >
 > and i want to know what really happens when the next code is executed
 >
 >   smartpointer<int> sp_int(new int);
 >   delete sp_int;
 >
 > I know there is a call for cast operator smartpointer* and then a call
 > to the overloaded delete operator, but is there any memory liberation
 > behind the scenes ?

Yes. delete sp_int will call your cast operator, then call the destructor on
that, then call your overloaded operator delete. Since your destructor deletes
your smart pointer, there will be memory released.

Over all, I would suggest you remove the cast operator. It's potentially unsafe
and could be applied in situations that you might not want it to be applied.

sm

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

smart pointer and overloaded delete operator

Post by Andy Sawye » Tue, 29 Jul 2003 08:30:08



 on 27 Jul 2003 06:47:37 -0400,


> ps. for a smart pointer implementation, look at auto_ptr's
> implementation, (usually found in the standard header <memory>).

Or even better, look at Boost's (http://www.boost.org) smart pointer
library.

Regards,
 Andy S
--
"Light thinks it travels faster than anything but it is wrong. No matter
 how fast light travels it finds the darkness has always got there first,
 and is waiting for it."                  -- Terry Pratchett, Reaper Man

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

smart pointer and overloaded delete operator

Post by Richard Smit » Wed, 30 Jul 2003 07:19:45


[code reformatted:]

Quote:>  > template <typename T> class smartpointer{
>  > protected:
>  >   T* m_pT;
>  > public:
>  >   smartpointer(T* pT=0):m_pT(pT) {}
>  >  ~smartpointer() { delete(m_pT); m_pT = 0; }
>  >   operator smartpointer*() { return(this); }
>  >   static void operator delete(void* pointer)    {}
>  > };

>  > and i want to know what really happens when the next code is executed

>  >   smartpointer<int> sp_int(new int);
>  >   delete sp_int;

> Yes. delete sp_int will call your cast operator, then call the destructor on
> that, then call your overloaded operator delete. Since your destructor deletes
> your smart pointer, there will be memory released.

But then, when sp_int goes out of scope, you call the
destructor again.  This is not legal, even though it is
likely to be safe in many real compiler.   Setting the
pointer to NULL in the destructor does not make it legal to
double-destruct the class; it merely makes it likely that
you won't get any observable problems from it.

In short, don't do it.  It's not legal, it's not idiomatic
and it's very confusing.

--
Richard Smith

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

1. conversion operator from a smart pointer to a regular pointer

template <class CReferred>
class TSmartPtr
{
    // ...
    public:

        operator CReferred*() const
        {
                return referred_item;
        }
    // ...

    private:
        // ...
        CReferred* referred_item;
        // ...

Warning: Didn't test it, didn't compile it.
But I think it can be dangerous. OP will certainly react.

IceT

2. FAQ

3. Overloading of global operator new and global operator delete

4. Site Critique

5. If I write a postfix operator++ overload in class and prefix operator++ overload outside the class..

6. *** Official release of UUCP V2.1b ***

7. delete operator function vesus operator delete

8. BEA M3

9. Ref. Counting Smart Pointer: Conversion to Base class Smart Ptr, How?

10. smart pointers aren't so smart

11. "Smart Pointers" Not So Smart?

12. My smart pointers aren't so smart

13. "Smart Pointers" Not So Smart?