Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-12-30 19:19:49


Adam Peterson wrote:
> Essentially, I'm passing a function a scoped_array and I want to
> assign data to it. It seems like there isn't a way to copy a chunk
> of data to the scoped_array except when it is created. Functionaly
> I'm where I want to be, but I'm thinking I must be misunderstanding
> the correct way to do this. I'm getting the data from a DLL that
> allocates its own memory, so I have to copy the data locally, then
> call the DLL's function to release the memory. Is this the best way
> to accomplish what I'm after?

It seems to me that you've missed scoped_array::reset:

> scoped_array<APPRIGHTITEM> saTemp ( sap );
> saGroups.swap( saTemp );

is exactly what saGroups.reset( sap ) would do.

You could also return a shared_array to avoid the local copy:

shared_array<APPRIGHTITEM> blah( ... )
{
    LPAPPRIGHTITEM lp = NULL;
    DWORD dw = 0;

    get_my_data( &lp, &dw );

    if( lp )
    {
        return shared_array<APPRIGHTITEM>( lp, FreeList );
    }
    else
    {
        return shared_array<APPRIGHTITEM>();
    }
}

Depending on how get_my_data and FreeList are specified, you may not need
the if. This approach also makes the code exception-safe; the original seems
to leak the list when new throws.

And of course if you needed the item count, you would've returned a
std::vector. :-)


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net