← Back to team overview

dolfin team mailing list archive

Re: smart pointers

 


Matthew Knepley wrote:
> On 10/23/06, Anders Logg <logg@xxxxxxxxx> wrote:
>>
>> On Mon, Oct 23, 2006 at 02:57:27PM +0200, Garth N. Wells wrote:
>> > What does everyone think about using some of the smart pointers from
>> > Boost? Instead of
>> >
>> >   int* nodes = new int[element.spacedim()];
>> >   element.nodemap(nodes, *cell, mesh);
>> >   /*
>> >      Do something
>> >   */
>> >    delete [] nodes;
>> >
>> > we would have
>> >
>> >   boost::scoped_array<int> nodes( new int[element.spacedim()] );
>> >   element.nodemap(nodes.get(), *cell, mesh);
>> >   /*
>> >      Do something
>> >   */
>> >
>> > In the end, it's pretty similar to std::vector, except it's a bit more
>> > efficient since it's not designed to change size and we can access the
>> > underlying pointer with get().
>> >
>> > Garth
>>
>> To me, it looks like code obfuscation. If you compare the two ways of
>> writing, you remove
>>
>>   "int*", "delete [] nodes"
>>
>> and add
>>
>>   "boost::scoped_array<int>", ".get()", ".get()" (get in two places here)
>>
>> There are probably places where we could make good use of smart
>> pointers, but here it looks to me like it just adds complexity.
>>
>> In our experience, just taking care of sizes is not usually enough. We
> want reference
> counting in there too, so we wrote our own (they do not have array sizes).

Boost has a bunch of neat smart pointers, depending on what you want.
boost::shared_prt (for single objects) and boost::shared_array (for
arrays) both do reference counting. We will need reference counting if
we use smart pointers in combination with different sub-functions
sharing the same vector and other data.

Garth

> However,
> I will note that you can remove all the get() calls with a suitably defined
> cast operator.
> 
>  Matt
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> DOLFIN-dev mailing list
> DOLFIN-dev@xxxxxxxxxx
> http://www.fenics.org/mailman/listinfo/dolfin-dev



References