Jump to content



delete an array of pointers access violation

  • You cannot reply to this topic
5 replies to this topic

#1 Naraxxi   Members   -  Reputation: 100

Like
0Likes
Like

Posted 07 February 2012 - 03:32 PM

hi ! Posted Image

code:
#include "stdafx.h"
#include <iostream>

using namespace std;


//Klasse MetallScheibe
class MetallScheibe
{

	protected:
		static const double PI; //Wie initialisiert man eine Klassenvariable richtig ?
		double radius;

	public:
		MetallScheibe(double radius) 
		{
			this->radius = radius;
		}

		double getRadius() const
		{
			return radius;
		}

		virtual double flaeche() //was macht das virtual ?
		 {
			return PI * radius *radius;
		}


		bool isSchwererAls(MetallScheibe* ms)
		{
			return flaeche() > ms->flaeche();
		}
};
const double MetallScheibe::PI = 3.14152;


//Klasse Sieb
class Sieb : public MetallScheibe
{
	private:
	int anzahlLoecher;
	int maxAnzahlLoecher;
	double lochRadius;
	MetallScheibe** loch;

public:
	//Einen Konstruktor
	Sieb(double radius, int maxAnzahlLoecher)  : MetallScheibe(radius)
	{
		this->maxAnzahlLoecher = maxAnzahlLoecher;		
		loch = new MetallScheibe* [maxAnzahlLoecher];

		this->lochRadius = radius/maxAnzahlLoecher;
		this->anzahlLoecher = 0;
	}

	void neuesLochStanzen() throw (out_of_range) //diese methode wirft eine out_of_range exception
	{
		if(anzahlLoecher + 1 > maxAnzahlLoecher)
			throw out_of_range("max Anzahl Loecher erreicht");
		else
		{			
			loch[anzahlLoecher] = new MetallScheibe(lochRadius);
			anzahlLoecher++;
		}
	}

	double flaeche()
	{
		return MetallScheibe::flaeche() - (anzahlLoecher * (PI * lochRadius * lochRadius));
	}

	void deleteLoecher()
	{
		//Vorerst müssen alle einzelen dynamisch angelegten Pointer-MetallScheiben-Objekte aus loch entfernt werden
		for (int i = 0; i < anzahlLoecher; i++)
		{
			delete [] loch[i];
		}

		//Schlussendlich müssen wir noch den "pointer auf ein Feld von pointern" selbst köschen
		delete[] loch;
	}

	//Einen Destruktor, der den dynamisch angelegten Speicherplatz freigibt
	~Sieb()
	{
		deleteLoecher();
	}

	//Einen Kopierkonstruktor...Hier werden die Attribute dieses Objektes von einem anderen sieb überschrieben
	Sieb (const Sieb& sieb) : MetallScheibe(sieb.radius)  // auch beim Kopierkonstruktor muss der Konstruktor der Basisklasse aufgerufen werden
	{
		if (this != &sieb)
		{
			anzahlLoecher = sieb.anzahlLoecher;
			maxAnzahlLoecher = sieb.maxAnzahlLoecher;
			lochRadius = sieb.radius/maxAnzahlLoecher;

			loch = new MetallScheibe*[maxAnzahlLoecher];
			//auch alle Loecher des Siebes müssen kopiert werden !
			for (int i = 0; i < sieb.anzahlLoecher; i++)
			{
				loch[i] = new MetallScheibe(sieb.lochRadius);
			}
		}
	}

	//Einen überladenen Zuweisungsoperator
	Sieb& operator= (const Sieb& sieb)
	{
		if(this != &sieb)
		{
			anzahlLoecher = sieb.anzahlLoecher;
			maxAnzahlLoecher = sieb.maxAnzahlLoecher;
			lochRadius = sieb.radius/maxAnzahlLoecher;

			//im Gegensatz zum Kopierkonstruktor muss hier die
			//dynamisch erzeutgen MetallScheiben gelöscht werden,
			//da operator= auf bereits erzeugt und inizialisierte
			//objekte angewendet wird
			
			deleteLoecher(); //WHAT'S WRONG ???

			loch = new MetallScheibe*[maxAnzahlLoecher];
			for (int i = 0; i < sieb.anzahlLoecher; i++)
			{
				loch[i] = new MetallScheibe(sieb.lochRadius);
			}
		}

		return *this;
	}
};


//TODO: Globale Funktion lochen()
Sieb& lochen(MetallScheibe ms, int maxAnzahl, int anzahlLoecher) throw (out_of_range)
{
	
	if(anzahlLoecher+1 > maxAnzahl) 
	{
		throw out_of_range("max Anzahl Löcher erreicht");
	}
	else
	{	
		Sieb* sieb = new Sieb(ms.getRadius(), maxAnzahl);
		for (int i; i < anzahlLoecher; i++)
			sieb->neuesLochStanzen();

		return *sieb;
	}
	
}


int _tmain(int argc, _TCHAR* argv[])
{

	Sieb sieb(2.0f,3);
	sieb.neuesLochStanzen();
	sieb.neuesLochStanzen();
	
	cout << sieb.flaeche() << endl;

	Sieb sieb2(2.0f,4); 
	sieb2 = sieb;

	cout << sieb2.flaeche() << endl;

	



	char f;
	cin >> f;
	return 0;
}
i am having problems using the destructor's "deleteLoecher" method in code line: 126
get an access violation and i guess it is because i am trying to delete memory that isn't dynamicly allocated.

In line: 54 i am allocating an array of pointers to the variable "MetallScheibe** loch".
In the class function "neuesLochStanzen" in line: 66 i "think" i am allocating memory for a new MetallScheibe() object.

is there something wrong with my allocation ?</iostream>

Ad:

#2 Cornstalks   Members   -  Reputation: 1216

Like
1Likes
Like

Posted 07 February 2012 - 03:46 PM

You are using delete [] to delete something that you newd (i.e. which you didn't new []). Line 66, you are allocating space for one object. But in line 81, you are trying to delete memory for many objects. delete [] what you new [], and delete what you new.
[ Realistic Rendering ] [ School + Dublin = Boom ] [ I've been ninja'd 70 times ] [ f.k.a. MikeTacular ] [ My Blog ]

#3 Naraxxi   Members   -  Reputation: 100

Like
0Likes
Like

Posted 07 February 2012 - 03:51 PM

i am sorry i found my mistake..it's in the overloaded operator= method ...i copy some data of the newly assigned object in advance BEFORE i delete the old allocated memory.
(One of it was an index variable)

I "think" it should be right now ! ^^

@Cornstalks:
Thank you for your reply.

in line 66 i do this:

loch[anzahlLoecher] = new MetallScheibe(lochRadius);

so i am assigning memory to one of the indexes of loch[] which happens to be all pointers

#4 Cornstalks   Members   -  Reputation: 1216

Like
1Likes
Like

Posted 07 February 2012 - 04:04 PM

View PostNaraxxi, on 07 February 2012 - 03:51 PM, said:

in line 66 i do this:

loch[anzahlLoecher] = new MetallScheibe(lochRadius);

so i am assigning memory to one of the indexes of loch[] which happens to be all pointers
Yes, but you still shouldn't delete [] loch[i]. You should delete loch[i] in line 86.

(and if you want line numbers, you can use source tags)
[ Realistic Rendering ] [ School + Dublin = Boom ] [ I've been ninja'd 70 times ] [ f.k.a. MikeTacular ] [ My Blog ]

#5 Naraxxi   Members   -  Reputation: 100

Like
0Likes
Like

Posted 07 February 2012 - 04:52 PM

okay, thank you Corntstalks..
I hope it is right now ^^

void deleteLoecher()
{
  //Vorerst müssen alle einzelen dynamisch angelegten Pointer-MetallScheiben-Objekte aus loch entfernt werden
  for (int i = 0; i < anzahlLoecher; i++)
  {
   delete loch[i];
  }
  //Schlussendlich müssen wir noch den "pointer auf ein Feld von pointern" selbst köschen
  delete[] loch;
}


#6 edd²   Members   -  Reputation: 1170

Like
1Likes
Like

Posted 07 February 2012 - 06:28 PM

Aside: Your value for Pi is wrong!

Slightly less aside: consider using std::vector<> and shared_ptr<>.






We are working on generating results for this topic
PARTNERS