Skip to main content
GameDev.net gamedev.net
🔒 Locked

delete an array of pointers access violation

Started by CoB_ChrizZz Feb 7, 2012 at 9:32 PM 4 replies 1.3k views
Original Post
CoB_ChrizZz
CoB_ChrizZz
hi ! smile.png

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

//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 = 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 = 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 ?
Cornstalks
Cornstalks
You are using [font=courier new,courier,monospace]delete [][/font] to delete something that you [font=courier new,courier,monospace]new[/font]d (i.e. which you didn't [font=courier new,courier,monospace]new [][/font]). Line 66, you are allocating space for one object. But in line 81, you are trying to delete memory for many objects. [font=courier new,courier,monospace]delete [][/font] what you [font=courier new,courier,monospace]new [][/font], and [font=courier new,courier,monospace]delete[/font] what you [font=courier new,courier,monospace]new[/font].
CoB_ChrizZz
CoB_ChrizZz
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
Cornstalks
Cornstalks

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 [font=courier new,courier,monospace]delete [] loch[/font]. You should [font=courier new,courier,monospace]delete loch[/font] in line 86.

(and if you want line numbers, you can use source tags)
CoB_ChrizZz
CoB_ChrizZz
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;
}
//Schlussendlich müssen wir noch den "pointer auf ein Feld von pointern" selbst köschen
delete[] loch;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.