[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] sletning af stl vector indeholdende dynamisk allokeret array



Måske hjelper dette her (noget refcounting). Ellers er der jo auto_ptr,
som vist tidligere.

Hans-Christian Stadler

PS:

sslug@sslug:~/tmp$ valgrind --leak-check=yes a.out
==2268== Memcheck, a memory error detector for x86-linux.
==2268== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.
==2268== Using valgrind-2.2.0, a program supervision framework for x86-linux.
==2268== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
==2268== For more details, rerun with: -v
==2268==
length test_vec :1000
length test_vec :1000
==2268==
==2268== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 1)
==2268== malloc/free: in use at exit: 6848 bytes in 3 blocks.
==2268== malloc/free: 4008 allocs, 4005 frees, 78784 bytes allocated.
==2268== For counts of detected errors, rerun with: -v
==2268== searching for pointers to 3 not-freed blocks.
==2268== checked 2394244 bytes.
==2268==
==2268== LEAK SUMMARY:
==2268==    definitely lost: 0 bytes in 0 blocks.
==2268==    possibly lost:   0 bytes in 0 blocks.
==2268==    still reachable: 6848 bytes in 3 blocks.
==2268==         suppressed: 0 bytes in 0 blocks.
==2268== Reachable blocks (those to which a pointer was found) are not shown.
==2268== To see them, rerun with: --show-reachable=yes

---------------------------------------------
#include <iostream>
#include <vector>

namespace {
 class Test {
  int i;
  double *vec;
  Test (const Test&);
  Test& operator= (const Test&);
 public:
  explicit Test (int n) : i(1), vec(new double[n]) {};
  ~Test () { delete[] vec; }
  
  friend class TestProxy;
 };
 
 class TestProxy {
  Test *t;
 public:
  explicit TestProxy (int n) : t(new Test(n)) {}
  TestProxy (const TestProxy &tp) : t(tp.t) { ++t->i; }
  ~TestProxy () { if (! --t->i) delete t; }
  
  TestProxy& operator= (const TestProxy &tp) {
   if (this == &tp)
    return *this;
   if (! --t->i)
    delete t;
   t = tp.t; ++t->i;
   return *this;
  }
  operator Test& () const { return *t; }
  double& operator[] (unsigned int k) const { return t->vec[k]; }
 };
}

int main(int argc, char *argv[])
{
 using namespace std;

 vector<TestProxy> test_vec;
 for (int i=0; i<1000; ++i)
  test_vec.push_back(TestProxy(3));
 cout << "length test_vec :" << test_vec.size() << endl;
 test_vec[0][0] = 7;
 test_vec.clear();
 for (int i=0; i<1000; ++i)
  test_vec.push_back(TestProxy(3));
 cout << "length test_vec :" << test_vec.size() << endl;
 test_vec.clear();
 return 0;
}



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:44 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *