[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



Hej til jeg begge, tak for alle indlæggene. Jeg har vedlagt nogle eksempler,
jeg håber at de er mere forståelige end mit første indlæg.

Jakob Oestergaard wrote:

> On Fri, Oct 29, 2004 at 12:21:52AM +0200, Rune Larsen wrote:
>> lang lang title.
>> Jeg har et lidt mystisk problem mht til new og delete operatorene. Jeg
>> har en stl vektor der indeholder objekter der har brugt new operatoren.
>> Problemet er at jeg gerne vil kunne opdatere vektoren. Jeg bruger clear()
>> og bygger hele vektoren op igen med andre objekter, men der opstår en
>> memory-leak fordi de dynamisk allokerede områder ikke bliver slettet.
> 
> Yep, klart. Pointerne (altså dine elementer) bliver slettet, men det de
> peger på sker der ikke noget med (det gør der jo aldrig, når en pointer
> deallokeres).
> 
>> Hvis
>> jeg bruger "delete []" i objekternes destructor'en hænger programmet når
>> vektoren skal oprettes .... jeg aner ikke hvorfor ? .
> Jeg forstår ikke hvad du mener... Kan du give et eksempel?

jeps her kommer det:

#include <iostream>
#include <vector>
using namespace std;
class Test
{
 public:
  Test(int n);
  ~Test();
 private:
  double *vec;
  double tmp;
};

Test::Test(int n){vec = new double[n];tmp=n;}
Test::~Test(){delete [] vec;}

int main(int argc, char *argv[])
{
  vector<Test> test_vec;
  for(unsigned int i=0;i<1000;i++)
    test_vec.push_back(Test(3));
  cout << "length test_vec :" << test_vec.size() << endl;
  return 0;
}

hvis man kører det ovenstående program tager det enten forfærdelig lang tid
eller også hænger det. Backtrace i gdb giver:

#0  0x4018ff1c in posix_memalign () from /lib/tls/libc.so.6
#1  0x4018d777 in mallopt () from /lib/tls/libc.so.6
#2  0x4018b4bf in malloc () from /lib/tls/libc.so.6
#3  0x400c148e in operator new(unsigned) () from /usr/lib/libstdc++.so.5
#4  0x400acb79 in std::__default_alloc_template<true,
0>::_S_chunk_alloc(unsigned, int&) () from /usr/lib/libstdc++.so.5
#5  0x400aca6d in std::__default_alloc_template<true,
0>::_S_refill(unsigned) () from /usr/lib/libstdc++.so.5
#6  0x400ac662 in std::__default_alloc_template<true, 0>::allocate(unsigned)
() from /usr/lib/libstdc++.so.5
#7  0x080494f2 in std::__simple_alloc<Test,
std::__default_alloc_template<true, 0> >::allocate(unsigned) (__n=4) at
stl_alloc.h:232
#8  0x080494c1 in std::_Vector_alloc_base<Test, std::allocator<Test>,
true>::_M_allocate(unsigned) (this=0xbffff350, __n=4)
    at stl_vector.h:127
#9  0x08049063 in std::vector<Test, std::allocator<Test>
>::_M_insert_aux(__gnu_cxx::__normal_iterator<Test*, std::vector<Test,
std::allocator<Test> > >, Test const&) (this=0xbffff350, __position=
      {<iterator<std::random_access_iterator_tag,Test,int,Test*,Test&>> =
{<No data fields>}, _M_current = 0x804b180}, sslug@sslug)
    at vector.tcc:236
#10 0x08048ce3 in std::vector<Test, std::allocator<Test> >::push_back(Test
const&) (this=0xbffff350, sslug@sslug)
    at stl_vector.h:603
#11 0x08048acb in main (argc=1, argv=0xbffff3f4) at test.cpp:28


>> Jeg kan undgå hukommelses-lækken hvis jeg laver en clean-funktion i
>> objekterne der ryder hukommelsen med "delete[]" før jeg kalder clear().
>> Men det får programmet til at hænge i en "new" der bliver kaldt efter
>> vektoren er oprettet.
> 
> Det forstår jeg heller ikke - kan du give nogle simple eksempler

Jeps. Den nedestående kode kører meget hurtigere, og det virker ? jeg ved
ikke hvorfor det er gået galt i mit originale program. 

#include <iostream>
#include <vector>

using namespace std;
class Test2
{
 public:
  Test2(int n);
  ~Test2();
  void clean_vec(); // <---- CLEAN FUNKTION
 private:
  double *vec;
  double tmp;
};

Test2::Test2(int n){vec = new double[n];tmp=n;}
Test2::~Test2(){}
void Test2::clean_vec(){delete [] vec;}

int main(int argc, char *argv[])
{
  vector<Test2> test_vec;
  double (*tmp_array)[3];
  
  for(unsigned int i=0;i<1000;i++)
    test_vec.push_back(Test2(3));
  
  tmp_array = new double[1000][3];  
  delete [] tmp_array;
  
  cout << "length test_vec :" << test_vec.size() << endl;
  
  for(unsigned int i=0;i<1000;i++) // clean kaldes før clear
    test_vec[i].clean_vec();       
  test_vec.clear();
  
  cout << "length test_vec :" << test_vec.size() << endl;
  for(unsigned int i=0;i<1000;i++)
    test_vec.push_back(Test2(3));
  cout << "length test_vec :" << test_vec.size() << endl;
  
  cout << "allokere nyt array " << endl; 
  tmp_array = new double[1000][3];// DET ER HER DET GÅR GALT
                                  // I MIT ORIGINALE PROGRAM
  cout << "sletter array  " << endl;
  delete [] tmp_array;
  return 0;
}

output:
length test_vec :1000
length test_vec :0
length test_vec :1000
allokere nyt array
sletter array


Jeg håber at det er blevet lidt mere forståeligt
hilsen rune




 
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] *