This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++ and garbage collection


On 5 February 2011 14:46, Enrico Weigelt wrote:
>> >
>> >> One viable approach is to modify the uses of pointers into shared_ptr
>> >> (from TR1 or boost) and then add the Boehm collector. ?This process
>> >> takes work, because changing all pointers won't work and changing
>> >> none won't buy you anything.
>> >
>> > Guess this would take a lot of work and add more dependencies
>> > (than just the relatively small boehm-gc lib) ...
>>
>> GCC includes a tr1::shared_ptr so there's no extra dependency if you
>> are only planning to use g++, and std::shared_ptr is part of C++0x.
>
> How does that one actually work and what do I have to do to use it ?

#include <tr1/memory>

int main()
{
  using std::tr1::shared_ptr;

  shared_ptr<int> p(new int);
  *p = 5;
  assert( *p == 5 );
  shared_ptr<int> p2(p);
  assert( p == p2 );
  assert( p.use_count() == 2 );
  p.reset();
  assert( p.use_count() == 0 );
  assert( p2.use_count() == 1 );
} // int deleted here

for more info read the docs for boost::shared_ptr (where the class
originated) or search the web, there is plenty of info on shared_ptr.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]