What's a smart pointer?

Discussion in 'C++' started by Gernot Frisch, Sep 21, 2004.

  1. and what for would I need it?

    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
     
    Gernot Frisch, Sep 21, 2004
    #1
    1. Advertising

  2. Gernot Frisch

    Sharad Kala Guest

    "Gernot Frisch" <> wrote in message
    news:...
    > and what for would I need it?


    It is a simple wrapper around a regular pointer. Typically operator * and ->
    are overloaded in such a class. One very simple example would be like -
    template <class T>
    class SPtr
    {
    T* ptr;
    public:
    SPtr(T* p = 0) : ptr(p) {}
    ~SPtr() {delete ptr;}
    T& operator*() {return *ptr;}
    T* operator->() {return ptr;}
    // ...
    };


    They give feel like raw (or dumb) pointers but one can give semantics to
    - Their construction and destruction
    - Copying and assignment
    - Dereferencing etc

    The topic can be best described only by a good text.
    Some good references to smart pointers are
    - More effective C++ (Meyers)
    - Modern C++ design (Alexandrescu)
    - Boost smart pointers

    Sharad
     
    Sharad Kala, Sep 21, 2004
    #2
    1. Advertising

  3. Gernot Frisch

    PKH Guest

    "Sharad Kala" <> wrote in message
    news:...
    >
    > "Gernot Frisch" <> wrote in message
    > news:...
    >> and what for would I need it?

    >
    > It is a simple wrapper around a regular pointer. Typically operator *
    > and ->
    > are overloaded in such a class. One very simple example would be like -
    > template <class T>
    > class SPtr
    > {
    > T* ptr;
    > public:
    > SPtr(T* p = 0) : ptr(p) {}
    > ~SPtr() {delete ptr;}
    > T& operator*() {return *ptr;}
    > T* operator->() {return ptr;}
    > // ...
    > };
    >
    >
    > They give feel like raw (or dumb) pointers but one can give semantics to
    > - Their construction and destruction
    > - Copying and assignment
    > - Dereferencing etc
    >
    > The topic can be best described only by a good text.
    > Some good references to smart pointers are
    > - More effective C++ (Meyers)
    > - Modern C++ design (Alexandrescu)
    > - Boost smart pointers
    >
    > Sharad
    >


    They are very useful if you integrate them with the delete-operator, so that
    when deleting memory, all smartpointers that point to that memory will be
    invalidated by setting their object-pointers to NULL.
    By using a hashtable for memory locations with smartpointers, the
    performance hit is low.

    PKH
     
    PKH, Sep 21, 2004
    #3
  4. Gernot Frisch

    Method Man Guest

    "PKH" <> wrote in message
    news:hqU3d.10665$g%...
    >
    > "Sharad Kala" <> wrote in message
    > news:...
    > >
    > > "Gernot Frisch" <> wrote in message
    > > news:...
    > >> and what for would I need it?

    > >
    > > It is a simple wrapper around a regular pointer. Typically operator *
    > > and ->
    > > are overloaded in such a class. One very simple example would be like -
    > > template <class T>
    > > class SPtr
    > > {
    > > T* ptr;
    > > public:
    > > SPtr(T* p = 0) : ptr(p) {}
    > > ~SPtr() {delete ptr;}
    > > T& operator*() {return *ptr;}
    > > T* operator->() {return ptr;}
    > > // ...
    > > };
    > >
    > >
    > > They give feel like raw (or dumb) pointers but one can give semantics to
    > > - Their construction and destruction
    > > - Copying and assignment
    > > - Dereferencing etc
    > >
    > > The topic can be best described only by a good text.
    > > Some good references to smart pointers are
    > > - More effective C++ (Meyers)
    > > - Modern C++ design (Alexandrescu)
    > > - Boost smart pointers
    > >
    > > Sharad
    > >

    >
    > They are very useful if you integrate them with the delete-operator, so

    that
    > when deleting memory, all smartpointers that point to that memory will be
    > invalidated by setting their object-pointers to NULL.
    > By using a hashtable for memory locations with smartpointers, the
    > performance hit is low.
    >
    > PKH
    >


    So are 'handles' equivalent to smart pointers created/managed by the OS?
     
    Method Man, Sep 22, 2004
    #4
  5. Method Man wrote:

    > So are 'handles' equivalent to smart pointers created/managed by the OS?



    When you are saying handles, are you talking about .NET/CLI?



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Sep 22, 2004
    #5
  6. Gernot Frisch

    Method Man Guest

    "Ioannis Vranos" <> wrote in message
    news:ciqe53$1iiv$...
    > Method Man wrote:
    >
    > > So are 'handles' equivalent to smart pointers created/managed by the OS?

    >
    >
    > When you are saying handles, are you talking about .NET/CLI?
    >


    I was being very vague, but I guess I was referring to handles in Win32
    programming (handle to a window) or COM objects. My knowledge of handles is
    very little, but they sound exactly like smart pointers to me.
     
    Method Man, Sep 22, 2004
    #6
  7. Gernot Frisch

    PKH Guest

    "Method Man" <> wrote in message
    news:XY94d.1150$...
    >
    > "Ioannis Vranos" <> wrote in message
    > news:ciqe53$1iiv$...
    >> Method Man wrote:
    >>
    >> > So are 'handles' equivalent to smart pointers created/managed by the
    >> > OS?

    >>
    >>
    >> When you are saying handles, are you talking about .NET/CLI?
    >>

    >
    > I was being very vague, but I guess I was referring to handles in Win32
    > programming (handle to a window) or COM objects. My knowledge of handles
    > is
    > very little, but they sound exactly like smart pointers to me.
    >
    >


    It's not quite the same. Handles in Win32 use reference counting to figure
    out how many has pointers to the data and only releases when the reference
    count reaches 0.
    With smartpointers, an allocation can have many smartpointers pointing to it
    when it is deleted. When integrated with delete, all these smartpointers
    will be invalidated. I.E. using f.ex pcSmartPtr->GetObject() now returns
    NULL for all of them. When using smartpointers you allways have to use a
    function like GetObject and check the returnvalue when accessing data.

    PKH
     
    PKH, Sep 22, 2004
    #7
  8. Gernot Frisch

    Howard Guest

    "PKH" <> wrote in message
    news:X9a4d.10819$g%...
    >
    >
    > It's not quite the same. Handles in Win32 use reference counting to figure
    > out how many has pointers to the data and only releases when the reference
    > count reaches 0.


    While you're correct that that is how handles are *used* in Windows, the
    HANDLE data type itself, in C++ (and C) under Windows, is defined as void*.
    It's how you obtain, release, and use them that makes the difference. (It
    would be a mistake, for example, to use new and delete on one.) But the
    object itself is just a pointer to void.

    -Howard
     
    Howard, Sep 23, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    704
    Chris Torek
    Feb 4, 2005
  2. MotoK
    Replies:
    59
    Views:
    1,817
    Keith Thompson
    Sep 15, 2006
  3. coala
    Replies:
    3
    Views:
    375
    coala
    Sep 6, 2006
  4. coala
    Replies:
    1
    Views:
    594
    Victor Bazarov
    Sep 6, 2006
  5. Hicham Mouline
    Replies:
    100
    Views:
    2,085
    Noah Roberts
    Aug 25, 2009
Loading...

Share This Page