Delete and free all elements of ptr_vector

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Delete and free all elements of ptr_vector

Boost - Users mailing list
Hello,

in my project we have a simple ptr_vector that has a member function:

   void deleteElements ()
   {
      for ( CONTENT_T * elem : _content ) {
         assertion ( elem != NULL );
         delete ( elem );
      }
   }

I want to replace this ptr_vector with boost::ptr_vector. However, I haven't found a way to delete and free all pointers
stored in the container. How can I do that?

Thanks,
Florian
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Delete and free all elements of ptr_vector

Boost - Users mailing list
Den 07-09-2017 kl. 11:09 skrev Florian Lindner via Boost-users:

> Hello,
>
> in my project we have a simple ptr_vector that has a member function:
>
>     void deleteElements ()
>     {
>        for ( CONTENT_T * elem : _content ) {
>           assertion ( elem != NULL );
>           delete ( elem );
>        }
>     }
>
> I want to replace this ptr_vector with boost::ptr_vector. However, I haven't found a way to delete and free all pointers
> stored in the container. How can I do that?

boost::ptr_vector<Foo> container;
...
container.clear()

kind regards

-Thorsten
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Delete and free all elements of ptr_vector

Boost - Users mailing list
Complete example:

#include <iostream>
#include <boost/utility.hpp>
#include <boost/ptr_container/ptr_vector.hpp>

struct Foo : boost::noncopyable
{
Foo() { std::cout << "made a Foo\n"; }
~Foo() { std::cout << "destroyed a Foo\n"; }
};

int main() {

boost::ptr_vector<Foo> foos;

foos.push_back(new Foo());
foos.push_back(new Foo());

foos.clear();

}

On 7 September 2017 at 11:28, Thorsten Ottosen via Boost-users <[hidden email]> wrote:
Den 07-09-2017 kl. 11:09 skrev Florian Lindner via Boost-users:
Hello,

in my project we have a simple ptr_vector that has a member function:

    void deleteElements ()
    {
       for ( CONTENT_T * elem : _content ) {
          assertion ( elem != NULL );
          delete ( elem );
       }
    }

I want to replace this ptr_vector with boost::ptr_vector. However, I haven't found a way to delete and free all pointers
stored in the container. How can I do that?

boost::ptr_vector<Foo> container;
...
container.clear()

kind regards

-Thorsten

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


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Delete and free all elements of ptr_vector

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
Thanks for your answer!

One more question: Is there a way to store an incomplete (forward declared) type. I get the compiler error message:

/home/florian/precice/src/mesh/Group.cpp:70:45:   required from here
/usr/include/boost/ptr_container/detail/reversible_ptr_container.hpp:111:17: error: invalid use of incomplete type
'class precice::mesh::Vertex'
                 BOOST_ASSERT( typeid(*res) == typeid(*x) &&
In file included from /home/florian/precice/src/mesh/Group.cpp:1:0:
/home/florian/precice/src/mesh/Group.hpp:8:11: note: forward declaration of 'class precice::mesh::Vertex'
     class Vertex;
           ^~~~~~
In file included from /usr/include/boost/assert.hpp:58:0,
                 from /usr/include/boost/range/size.hpp:23,
                 from /usr/include/boost/range/functions.hpp:20,
                 from /usr/include/boost/ptr_container/detail/reversible_ptr_container.hpp:29,
                 from /usr/include/boost/ptr_container/ptr_sequence_adapter.hpp:20,
                 from /usr/include/boost/ptr_container/ptr_vector.hpp:20,
                 from /home/florian/precice/src/mesh/Group.hpp:4,
                 from /home/florian/precice/src/mesh/Group.cpp:1:
/usr/include/boost/ptr_container/detail/reversible_ptr_container.hpp:111:17: error: invalid use of incomplete type
'class precice::mesh::Vertex'
                 BOOST_ASSERT( typeid(*res) == typeid(*x) &&

So I assume no, or is there?

Thanks,
Florian

Am 07.09.2017 um 17:28 schrieb Thorsten Ottosen:

> Den 07-09-2017 kl. 11:09 skrev Florian Lindner via Boost-users:
>> Hello,
>>
>> in my project we have a simple ptr_vector that has a member function:
>>
>>     void deleteElements ()
>>     {
>>        for ( CONTENT_T * elem : _content ) {
>>           assertion ( elem != NULL );
>>           delete ( elem );
>>        }
>>     }
>>
>> I want to replace this ptr_vector with boost::ptr_vector. However, I haven't found a way to delete and free all pointers
>> stored in the container. How can I do that?
>
> boost::ptr_vector<Foo> container;
> ...
> container.clear()
>
> kind regards
>
> -Thorsten
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Delete and free all elements of ptr_vector

Boost - Users mailing list
Den 07-09-2017 kl. 12:57 skrev Florian Lindner via Boost-users:
> Thanks for your answer!
>
> One more question: Is there a way to store an incomplete (forward declared) type. I get the compiler error message:
>

Please see the FAQ:

http://www.boost.org/doc/libs/1_64_0/libs/ptr_container/doc/faq.html

Short answer: yes, but not at instantiation time.

-Thorsten
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Delete and free all elements of ptr_vector

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
Den 07-09-2017 kl. 11:09 skrev Florian Lindner via Boost-users:

> Hello,
>
> in my project we have a simple ptr_vector that has a member function:
>
>     void deleteElements ()
>     {
>        for ( CONTENT_T * elem : _content ) {
>           assertion ( elem != NULL );
>           delete ( elem );
>        }
>     }
>
> I want to replace this ptr_vector with boost::ptr_vector. However, I haven't found a way to delete and free all pointers
> stored in the container. How can I do that?

Note also that the destructor automatically cleans up.

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