new and delete help for classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Renato

    #1

    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==(cons t Point &rhs);
    bool operator!=(cons t Point &rhs);
    bool operator<( const Point & rhs) { return this->y < rhs.y; }
    };

    class Shape
    {
    public:
    vector<PointpL;
    vector<Line eL;

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

    // Member functions
    void SetColour(Colou r);

    // 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(in t iX, int iY, Colour iCol)
    {
    x = iX;
    y = iY;
    col = iCol;
    }

    Point::Point(in t 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(Poin t iP1, Point iP2)
    {
    p1 = iP1;
    p2 = iP2;
    }

    string Line::GetInfo(v oid)
    {
    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::SetColou r(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_st r();

    return(s);
    }

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

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

    string Triangle::GetIn fo(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(v oid)
    {
    cout << "Polygon Draw" << endl;
    Triangle::Draw( );
    }

    string Polygon::GetInf o(void)
    {
    string s;
    s = "Polygon - GetInfo";
    s += Triangle::GetIn fo().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_st r() << 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 ==--
    }


  • Alf P. Steinbach

    #2
    Re: new and delete help for classes

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

    Comment

    • Renato

      #3
      Re: new and delete help for classes

      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.
      >
      >

      Comment

      • Jim Langston

        #4
        Re: new and delete help for classes

        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_bac k( 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" <rk333@iinet.ne t.auwrote in message
        news:460fa914$0 $15025$5a62ac22 @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.


        Comment

        • James Kanze

          #5
          Re: new and delete help for classes

          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: james.kanze@gma il.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • Renato

            #6
            Re: new and delete help for classes

            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_bac k( 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.

            :)

            Comment

            Working...