[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 dynamiskallokeret array



[klip]

#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;
}



[klip]
Jeg har ikke min C++-reference-bog lige her, men et hurtigt bud på, hvorfor det går galt, er at push_back ikke bruger referencer. For at gøre det mere forståeligt:


du kalder test_vec.push_back(Test(3))

Hvad kompileren opfatter:

1) Lav objektet Test med constructorværdi 3 (vi kalder objektet A). Der oprettes en double, der så peger på adresse X.
2) Nu kopieres objektet, fordi push_back ikke bruger referencer i funktionskaldet. Da du ikke har skrevet en copy constructor til din klasse, forsøger kompileren selv at kopiere det. Kopiering af en pointer er defineret til blot at lave en pointer, der peger på det samme. Derfor har vi nu to Test-objekter (A og B), der begge peger på adressen X.
3) Objekt A har nu overstået sin livstid, og destructoren bliver kaldt.
4) Objekt B bliver indsat i vektoren med en pointer der er invalid.
5) Når destructoren på objekt B bliver kaldt, går det galt.


Løsningen er, at lave en copy constructor for din klasse eller indsætte pointere i vectoren i stedet. Så skal du blot selv manuelt løbe igennem vektoren og destruere objekterne.

I øvrigt er push_back noget tung når du bruger den på den måde. Hvis du alligevel ved fra starten hvor mange elementer, der skal i, er det bedre at resize vektoren på en gang. Med push_back bliver den udvidet løbende hvilket giver en del kopiering af hukommelse.

Mange hilsner

Lars


-- ------------------------------------------------------------------------

Lars Riisgaard Ribe

MSc, PhD-stud.

Telephone



+45 89 49 52 68

Fax 	+45 89 49 60 04
Email 	sslug@sslug <mailto:sslug@sslug>
Address 	MR-Center - Aarhus University Hospital
Skejby Sygehus
Brendstrupgaardsvej
Dk-8200 Aarhus N
Denmark

------------------------------------------------------------------------

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