Smart Pointer help

Discussion in 'C++' started by amparikh@gmail.com, May 25, 2005.

  1. Guest

    Greetings all.
    I am sure people here might have written smart pointers in a way that
    can handle both a pointer to an object and also a pointer to an array
    of objects.

    so if you have smart pointer Ptr...

    and then you have

    Ptr<Element> a( new Element());
    Ptr<Element> b( new Element[]());

    so how does one differentiate between the two when doing the freeing of
    memory...basically differentiating delete and delete[].

    I have tried using delete[] on a pointer to single object and it sorta
    works, but I know this will cause issues, espeically when someone might
    have implemented the operator delete in their class(the class which is
    the held pointer in the smart pointer) and I am sure there are other
    situations, where this might fail.

    Thanks.
    , May 25, 2005
    #1
    1. Advertising

  2. John Carson Guest

    <> wrote in message
    news:
    > Greetings all.
    > I am sure people here might have written smart pointers in a way that
    > can handle both a pointer to an object and also a pointer to an array
    > of objects.
    >
    > so if you have smart pointer Ptr...
    >
    > and then you have
    >
    > Ptr<Element> a( new Element());
    > Ptr<Element> b( new Element[]());
    >
    > so how does one differentiate between the two when doing the freeing
    > of memory...basically differentiating delete and delete[].


    Having one pointer for both purposes seems like it would be hard and
    probably compiler specific. The simpler approach is to have two smart
    pointers; one for single objects and one for arrays. That is what Boost
    does.

    http://www.boost.org/libs/smart_ptr/smart_ptr.htm


    --
    John Carson
    John Carson, May 25, 2005
    #2
    1. Advertising

  3. John Carson wrote:
    >
    > Having one pointer for both purposes seems like it would be hard and
    > probably compiler specific. The simpler approach is to have two smart
    > pointers; one for single objects and one for arrays. That is what Boost
    > does.
    >
    > http://www.boost.org/libs/smart_ptr/smart_ptr.htm
    >


    Yes. Most that I've seen are even cheaper: they just say that you're not
    allowed to contain something allocated by new[].

    Jacques.
    Jacques Labuschagne, May 25, 2005
    #3
  4. Pete Becker Guest

    wrote:
    >
    > so how does one differentiate between the two when doing the freeing of
    > memory...basically differentiating delete and delete[].
    >


    The constructor for TR1's shared_ptr takes an optional argument that is
    a deleter for the object being managed. So you'd do this:

    void delete_array(Element* ptr)
    {
    delete [] ptr;
    }

    shared_ptr<Element> sp(new Element); // single element
    shared_ptr<Element> sp1(new Element[10], delete_array);

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, May 25, 2005
    #4
  5. John Carson wrote:

    >> so how does one differentiate between the two when doing the freeing
    >> of memory...basically differentiating delete and delete[].

    >
    > Having one pointer for both purposes seems like it would be hard and
    > probably compiler specific. The simpler approach is to have two smart
    > pointers; one for single objects and one for arrays. That is what Boost
    > does.


    Other solution is to have only one for delete [ ] and let the users do:

    new My [1];

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, May 25, 2005
    #5
  6. <> skrev i en meddelelse
    news:...
    > Greetings all.
    > I am sure people here might have written smart pointers in a way that
    > can handle both a pointer to an object and also a pointer to an array
    > of objects.
    >
    > so if you have smart pointer Ptr...
    >
    > and then you have
    >
    > Ptr<Element> a( new Element());
    > Ptr<Element> b( new Element[]());
    >
    > so how does one differentiate between the two when doing the freeing of
    > memory...basically differentiating delete and delete[].
    >
    > I have tried using delete[] on a pointer to single object and it sorta
    > works, but I know this will cause issues, espeically when someone might
    > have implemented the operator delete in their class(the class which is
    > the held pointer in the smart pointer) and I am sure there are other
    > situations, where this might fail.
    >
    > Thanks.
    >


    In the second case, you should probably use a std::vector.

    /Peter
    Peter Koch Larsen, May 25, 2005
    #6
  7. Rapscallion Guest

    wrote:
    > I am sure people here might have written smart pointers in a way that
    > can handle both a pointer to an object and also a pointer to an array
    > of objects.
    >
    > so if you have smart pointer Ptr...
    >
    > and then you have
    >
    > Ptr<Element> a( new Element());
    > Ptr<Element> b( new Element[]());
    >
    > so how does one differentiate between the two when doing the freeing of
    > memory...basically differentiating delete and delete[].


    Try to find out how Boost implements is_array<T>
    (http://www.boost.org/libs/type_traits/) and use it as second (default)
    parameter in the ctor. BTW, of course, smart people don't use smart
    pointers ;-)

    R.C.
    Rapscallion, May 25, 2005
    #7
  8. Amit Guest

    "Pete Becker" <> wrote in message
    news:...
    > wrote:
    > >
    > > so how does one differentiate between the two when doing the freeing of
    > > memory...basically differentiating delete and delete[].
    > >

    >
    > The constructor for TR1's shared_ptr takes an optional argument that is
    > a deleter for the object being managed. So you'd do this:
    >
    > void delete_array(Element* ptr)
    > {
    > delete [] ptr;
    > }
    >
    > shared_ptr<Element> sp(new Element); // single element
    > shared_ptr<Element> sp1(new Element[10], delete_array);


    Thats interesting. I myself was thinking of modifying my smart pointer
    implementation, where I have to send an extra parameter as the count of the
    objects, defaulted to 1.
    But if shared_ptr already does that, then I can use it.

    However, I am not sure if VS.Net 2003 has the support for all this. Have
    the TR1 extensions being added to VS.NET ?

    Thanks.

    >
    > --
    >
    > Pete Becker
    > Dinkumware, Ltd. (http://www.dinkumware.com)
    Amit, May 25, 2005
    #8
  9. Pete Becker Guest

    Amit wrote:
    >
    > However, I am not sure if VS.Net 2003 has the support for all this. Have
    > the TR1 extensions being added to VS.NET ?
    >


    I don't know of any compiler that comes with them presently. That's in
    part because TR1 hasn't gotten final approval yet, although that should
    happen in the next few months. In the meantime, the predecessor for
    TR1's shared_ptr is available from boost. It's probably very close to
    what was approved, but I haven't checked.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, May 25, 2005
    #9
    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. MotoK
    Replies:
    59
    Views:
    1,808
    Keith Thompson
    Sep 15, 2006
  2. Protoman

    Smart Pointer help

    Protoman, Jul 4, 2006, in forum: C++
    Replies:
    13
    Views:
    634
    Thomas J. Gritzan
    Jul 5, 2006
  3. coala
    Replies:
    3
    Views:
    373
    coala
    Sep 6, 2006
  4. coala
    Replies:
    1
    Views:
    590
    Victor Bazarov
    Sep 6, 2006
  5. Hicham Mouline
    Replies:
    100
    Views:
    2,081
    Noah Roberts
    Aug 25, 2009
Loading...

Share This Page