Re: how to use shared_ptr to wrap std::vector and returnfrom function?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: how to use shared_ptr to wrap std::vector and returnfrom function?

Sohail Somani
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of
> [hidden email]
> Sent: Monday, March 20, 2006 9:59 AM
> To: [hidden email]
> Subject: [Boost-users] how to use shared_ptr to wrap
> std::vector and returnfrom function?
>
> hi, Boost users
>
> Suppose I created a large data in a function and want to
> return it back to where the function was called. I want to
> return the pointer so that the deep copy of the large data is
> not needed and memory leak is nicely handled. Can someone let
> me know how to do it?

This is what auto_ptr is for, shared_ptr is overkill as far as I can
tell:

#include <iostream>
#include <memory>
#include <vector>

typedef std::auto_ptr<std::vector<double> > avd;

avd getV()
{
        avd a(new std::vector<double>);
    a->reserve(2);
        a->push_back(1.0);
        a->push_back(2.0);
        return a;
}

int main()
{
        const avd a = getV();
        std::vector<double> & ref = *a;
        std::cout << ref[0] << " " << ref[1] << std::endl;
        ref[0]=5;
        std::cout << ref[0] << " " << ref[1] << std::endl;
}
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: how to use shared_ptr to wrap std::vector and returnfrom function?

David Abrahams
"Sohail Somani" <[hidden email]> writes:

>> -----Original Message-----
>> From: [hidden email]
>> [mailto:[hidden email]] On Behalf Of
>> [hidden email]
>> Sent: Monday, March 20, 2006 9:59 AM
>> To: [hidden email]
>> Subject: [Boost-users] how to use shared_ptr to wrap
>> std::vector and returnfrom function?
>>
>> hi, Boost users
>>
>> Suppose I created a large data in a function and want to
>> return it back to where the function was called. I want to
>> return the pointer so that the deep copy of the large data is
>> not needed and memory leak is nicely handled. Can someone let
>> me know how to do it?
>
> This is what auto_ptr is for, shared_ptr is overkill as far as I can
> tell

unless you have to cross DLL boundaries, or you need a custom deletion
routine, or a base class of the object pointed to might not have a
virtual destructor, ...

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users