new and delete help for classes


R

Renato

I have an array of pointers to class Shape.

I create 4 items and display their values.

shapes[0 - 3]
Shape *shapes[SIZE];

shapes[0] = new Shape (p1);
shapes[1] = new Triangle (p1);
shapes[2] = new Polygon (p2);

p2.push_back( Point( 210, 210) );
shapes[3] = new Polygon (p2);

sListSize = 4;

// --== This is what is causing my program to crash - but why? ==--
// Please help
for(int i = 0; i < sListSize; i ++)
delete shapes;



But when I try to delete them, The program crashes after item 0.


Please find attached the code below:


//----------------------------------------------------------------------------------------------------------
// .h file
//----------------------------------------------------------------------------------------------------------
class Point
{
public:
int x, y;
Colour col;

public:
Point(void) { x = y = 100; col.r = col.g = col.b = 255; }
Point(int iX, int iY, Colour iCol);
Point(int iX, int iY);
string GetInfo(void);

// operators
Point operator=(const Point &rhs);
bool operator==(const Point &rhs);
bool operator!=(const Point &rhs);
bool operator<( const Point & rhs) { return this->y < rhs.y; }
};

class Shape
{
public:
vector<Point> pL;
vector<Line> eL;

public:
Shape(vector<Point> pList) : pL(pList) { cout << "Shape constructor" <<
endl; }
Shape(void) {}
~Shape() { cout << "Shape Destructor" << endl; }

// Member functions
void SetColour(Colour);

// Virtual functions
virtual string GetInfo(void);
virtual void Draw(void);

// Sorting routines
void SortOnY(void);
void SortOnX(void);
};

class Triangle : virtual public Shape
{
public:
Triangle(vector<Point> pList, unsigned int fType = 0) :
Shape(pList), fillType(fType) { SortOnY(); cout << "Triangle constructor"
<< endl; }

~Triangle() { cout << "Triangle destructor" << endl; }
void Draw(void);
string GetInfo(void);
private:
unsigned int fillType;
};

// Member of Shape and Triangle
class Polygon : virtual public Shape, public Triangle
{
public:
Polygon(vector<Point> pList, unsigned int fType = 0) :
Triangle(pList, fType), Shape(pList) { SortOnX(); }

~Polygon() { cout << "Polygon destructor" << endl; }
void Draw(void);
string GetInfo(void);
};

//----------------------------------------------------------------------------------------------------------
// .cpp file
//----------------------------------------------------------------------------------------------------------
Point::point(int iX, int iY, Colour iCol)
{
x = iX;
y = iY;
col = iCol;
}

Point::point(int iX, int iY)
{
x = iX;
y = iY;
col.r = col.b = col.g = 255;
}

string Point::GetInfo(void)
{
string s;
char buff[200];
sprintf(buff, "Point (%3d,%3d) - Col_RGB: [%3d,%3d,%3d]", x, y, col.r,
col.g, col.b);
s = buff;
return(s);
}

Point Point::eek:perator=(const Point &rhs)
{
if(this == &rhs)
return(*this);

x = rhs.x;
y = rhs.y;
col = rhs.col;

return(*this);
}

bool Point::eek:perator==(const Point &rhs)
{
if(rhs.x == x && rhs.y == y)
return(true);
else
return(false);
}

bool Point::eek:perator!=(const Point &rhs)
{
if(rhs.x != x && rhs.y != y)
return(true);
else
return(false);
}



// ------------------------------------------------------------------
// --== LINE ==--
// ------------------------------------------------------------------
// LINE --> Constructor 1
Line::Line(Point iP1, Point iP2)
{
p1 = iP1;
p2 = iP2;
}

string Line::GetInfo(void)
{
string s;
char buff[200];

return(s);
}


// ------------------------------------------------------------------
// --== SHAPE ==--
// ------------------------------------------------------------------
// Sorting functions
bool lessX(const Point &p1, const Point &p2) { return(p1.x < p2.x); }
bool lessY(const Point &p1, const Point &p2) { return(p1.y < p2.y); }

void Shape::SortOnX(void) { sort (pL.begin(), pL.end(), lessX); }
void Shape::SortOnY(void) { sort (pL.begin(), pL.end(), lessY); }

void Shape::SetColour(Colour c)
{
for(int i = 0; i < pL.size(); i ++)
pL.col = c;
}

string Shape::GetInfo(void)
{
string s;
char buff[200];
sprintf( buff, "Shape - pointCnt: %d", pL.size() );
s = buff;

for(int i = 0; i < pL.size(); ++ i)
s += string("\n\t") + pL.GetInfo().c_str();

return(s);
}

void Shape::Draw(void) { cout << "Shape Draw" << endl; }

// ------------------------------------------------------------------
// --== SHAPE ==--
// ------------------------------------------------------------------
void Triangle::Draw() { cout << "Triangle Draw" << endl; }

string Triangle::GetInfo(void)
{
string s;
char buff[200];
s = "Triangle - ";
sprintf(buff, "FillType: %d\n", fillType);
s += buff;
s += Shape::GetInfo().c_str();
return(s);
}

void Polygon::Draw(void)
{
cout << "Polygon Draw" << endl;
Triangle::Draw();
}

string Polygon::GetInfo(void)
{
string s;
s = "Polygon - GetInfo";
s += Triangle::GetInfo().c_str();
return(s);
}

//----------------------------------------------------------------------------------------------------------
// .cpp MAIN file
//----------------------------------------------------------------------------------------------------------
int sListSize = 0;

Shape *shapes[SIZE];

vector<Point> p1;
p1.push_back( Point(10, 10) );
p1.push_back( Point(100, 100) );
p1.push_back( Point(20, 20) );

shapes[0] = new Shape (p1);
shapes[1] = new Triangle (p1);
vector<Point> p2;
p2.push_back( Point(10, 10) );
p2.push_back( Point(100, 100) );
p2.push_back( Point(20, 20) );
p2.push_back( Point( 200, 200) );

//shapes[2] = new Polygon (p2);
shapes[2] = new Polygon (p2);

p2.push_back( Point(450, 120) );
shapes[3] = new Polygon (p2);
sListSize = 4;

cout << "\nDoing the old pointer list method: " << endl;
for(int i = 0; i < sListSize; i ++)
{
cout << "\n" << "I: " << i << " " << shapes->GetInfo().c_str() << endl;
shapes->Draw();
}


cout << "\n--== DELETING MEMORY ==--\n";
cout << "Size: " << sListSize << endl;

for(int i = 0; i < sListSize; i ++)
{
cout << "DEL: " << i << endl;
delete shapes; // --== This is causing me problems ==--
}
 
Ad

Advertisements

A

Alf P. Steinbach

* Renato:
I have an array of pointers to class Shape.

I create 4 items and display their values.

shapes[0 - 3]

This is not valid C++ syntax.

Shape *shapes[SIZE];

shapes[0] = new Shape (p1);
shapes[1] = new Triangle (p1);
shapes[2] = new Polygon (p2);

p2.push_back( Point( 210, 210) );
shapes[3] = new Polygon (p2);

sListSize = 4;

// --== This is what is causing my program to crash - but why? ==--
// Please help
for(int i = 0; i < sListSize; i ++)
delete shapes;


You need a virtual destructor.
 
R

Renato

I understand that shapes[0 - 3] is not correct C++ syntax, it was to show
the ascending progression of the assignments.

Shape *shapes[SIZE]; is declared within main.

Shape, Triangle and Polygon all have a standard destructor, there are no
pointers allocated within the classes, hence there was no need for an
explicit virtual destructor method. But I did provide one for both
Triangle and Polygon.

Please let me know if I am missing anything with these destructors.

I would like to clean up any memory allocations at the end of main.

Please provide some code for easier understanding if possible.

Regards
Renato
* Renato:
I have an array of pointers to class Shape.

I create 4 items and display their values.

shapes[0 - 3]

This is not valid C++ syntax.

Shape *shapes[SIZE];

shapes[0] = new Shape (p1);
shapes[1] = new Triangle (p1);
shapes[2] = new Polygon (p2);

p2.push_back( Point( 210, 210) );
shapes[3] = new Polygon (p2);

sListSize = 4;

// --== This is what is causing my program to crash - but why? ==--
// Please help
for(int i = 0; i < sListSize; i ++)
delete shapes;


You need a virtual destructor.
 
J

Jim Langston

Alf said:
* Renato:
I have an array of pointers to class Shape.

I create 4 items and display their values.

shapes[0 - 3]

This is not valid C++ syntax.

Shape *shapes[SIZE];

shapes[0] = new Shape (p1);
shapes[1] = new Triangle (p1);
shapes[2] = new Polygon (p2);

p2.push_back( Point( 210, 210) );
shapes[3] = new Polygon (p2);

sListSize = 4;

// --== This is what is causing my program to crash - but why? ==--
// Please help
for(int i = 0; i < sListSize; i ++)
delete shapes;


You need a virtual destructor.


Renato said:
I understand that shapes[0 - 3] is not correct C++ syntax, it was to show
the ascending progression of the assignments.

Shape *shapes[SIZE]; is declared within main.

Shape, Triangle and Polygon all have a standard destructor, there are no
pointers allocated within the classes, hence there was no need for an
explicit virtual destructor method. But I did provide one for both
Triangle and Polygon.

Please let me know if I am missing anything with these destructors.

I would like to clean up any memory allocations at the end of main.

Please provide some code for easier understanding if possible.

Please don't top-post. Message rearranged.

Triangle has an int that shape does not have. It is posible that with this
included variable the compiler is rearranging in memory, for what ever
reason, the variables of Shape ( in this case two vectors). The destructor
for Shape has to destroy these vectors which is probably where your error is
coming it, because the compiler thinks it's a Shape, but it's actually a
Triange, so the memory layout has changed.

The short answer, if you use polymorphism, ALWAYS give a virtual destructor.

Even if you give NO custom destructors (comment them out for Shape, Tirangle
and Polygon) it still crashes. So you have to provide a virtual destructor.
 
J

James Kanze

I understand that shapes[0 - 3] is not correct C++ syntax, it was to show
the ascending progression of the assignments.
Shape *shapes[SIZE]; is declared within main.
Shape, Triangle and Polygon all have a standard destructor, there are no
pointers allocated within the classes, hence there was no need for an
explicit virtual destructor method. But I did provide one for both
Triangle and Polygon.

Shape is a base class. You delete via a Shape*. Thus, Shape
must have a virtual destructor, or the behavior is undefined.
 
Ad

Advertisements

R

Renato

Renato said:
I understand that shapes[0 - 3] is not correct C++ syntax, it was to show
the ascending progression of the assignments.

Shape *shapes[SIZE]; is declared within main.

Shape, Triangle and Polygon all have a standard destructor, there are no
pointers allocated within the classes, hence there was no need for an
explicit virtual destructor method. But I did provide one for both
Triangle and Polygon.

Please let me know if I am missing anything with these destructors.

I would like to clean up any memory allocations at the end of main.

Please provide some code for easier understanding if possible.

Regards
Renato
* Renato:
I have an array of pointers to class Shape.

I create 4 items and display their values.

shapes[0 - 3]

This is not valid C++ syntax.

Shape *shapes[SIZE];

shapes[0] = new Shape (p1);
shapes[1] = new Triangle (p1);
shapes[2] = new Polygon (p2);

p2.push_back( Point( 210, 210) );
shapes[3] = new Polygon (p2);

sListSize = 4;

// --== This is what is causing my program to crash - but why? ==--
// Please help
for(int i = 0; i < sListSize; i ++)
delete shapes;


You need a virtual destructor.


Thank you to all who responded.

:)
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top