Smart Pointer help

A

amparikh

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.
 
J

John Carson

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
 
J

Jacques Labuschagne

John said:
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.
 
P

Pete Becker

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);
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

John said:
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];
 
P

Peter Koch Larsen

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
 
R

Rapscallion

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.
 
A

Amit

Pete Becker said:
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.
 
P

Pete Becker

Amit said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top