Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Smart Pointer help

Reply
Thread Tools

Smart Pointer help

 
 
amparikh@gmail.com
Guest
Posts: n/a
 
      05-25-2005
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.

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      05-25-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com
> 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

 
Reply With Quote
 
 
 
 
Jacques Labuschagne
Guest
Posts: n/a
 
      05-25-2005
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.
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      05-25-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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)
 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      05-25-2005
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
 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      05-25-2005

<(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed) ups.com...
> 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


 
Reply With Quote
 
Rapscallion
Guest
Posts: n/a
 
      05-25-2005
(E-Mail Removed) 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.

 
Reply With Quote
 
Amit
Guest
Posts: n/a
 
      05-25-2005

"Pete Becker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> (E-Mail Removed) 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)



 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      05-25-2005
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Never ever use a raw pointer when a smart pointer can do the same job Hicham Mouline C++ 100 08-25-2009 05:07 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 1 09-06-2006 03:00 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 3 09-06-2006 02:58 PM
Smart Card Certificate Logon and Smart Card Wireless EAP-TLS erha Wireless Networking 0 05-19-2005 01:40 AM
trade 64mb smart media for 16mb smart media cards wjva Digital Photography 1 08-20-2003 08:30 PM



Advertisments