[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



On Sat, Oct 30, 2004 at 04:30:54PM +0200, Rune Larsen wrote:
> 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.

Problemet i dit første program er, at du ikke har fået lavet en copy
constructor. Når du kalder push_back bliver der taget en kopi af det
objekt du pusher og uden en eksplicit copy constructor benyttes en
standard copy constructor. Problemet opstår så når du prøver at nedlægge
objektet, da standard copy constructoren ikke har fået initialiseret
*vec ordentligt, vil destructoren prøve at nedlægge et eller andet
vilkårligt.

Følgende program virker fint:

#include <iostream>
#include <vector>
using namespace std;
class Test
{
	public:
		Test(int n);
		Test(const Test &t); // copy constructor
		~Test();
		int tmp;
		double *vec;
};

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

	void foo() {
		Test *t;
		t = new Test(3);
		delete t;
	}

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

-- 
Med venlig hilsen
- Jacob Atzen


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