468,773 Members | 2,339 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,773 developers. It's quick & easy.

new and delete help for classes

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[i];

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<PointpL;
vector<Line eL;

public:
Shape(vector<PointpList) : 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<PointpList, 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<PointpList, 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::operator=(const Point &rhs)
{
if(this == &rhs)
return(*this);

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

return(*this);
}

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

bool Point::operator!=(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[i].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[i].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<Pointp1;
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<Pointp2;
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[i]->GetInfo().c_str() << endl;
shapes[i]->Draw();
}
cout << "\n--== DELETING MEMORY ==--\n";
cout << "Size: " << sListSize << endl;

for(int i = 0; i < sListSize; i ++)
{
cout << "DEL: " << i << endl;
delete shapes[i]; // --== This is causing me problems ==--
}
Apr 1 '07 #1
5 1826
* 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[i];
You need a virtual destructor.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 1 '07 #2
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

Alf P. Steinbach wrote:
* 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[i];

You need a virtual destructor.

Apr 1 '07 #3
Alf P. Steinbach wrote:
>* 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[i];

You need a virtual destructor.
"Renato" <rk***@iinet.net.auwrote in message
news:46***********************@per-qv1-newsreader-01.iinet.net.au...
>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.
Apr 1 '07 #4
On Apr 1, 2:44 pm, Renato <r...@iinet.net.auwrote:
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 1 '07 #5
Renato wrote:
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

Alf P. Steinbach wrote:
>* 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[i];

You need a virtual destructor.

Thank you to all who responded.

:)

Apr 1 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ian McBride | last post: by
6 posts views Thread by Alexander Stippler | last post: by
1 post views Thread by Nimmi Srivastav | last post: by
5 posts views Thread by | last post: by
2 posts views Thread by Pierre Phaneuf | last post: by
10 posts views Thread by n2xssvv g02gfr12930 | last post: by
16 posts views Thread by Martin Vorbrodt | last post: by
2 posts views Thread by shapper | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.