-1

I looked at a couple of the other questions surrounding the glibc detected error, however my code seems a little different. I believe the error is in my tetrahedron class since if I comment it out along with the calls to it, the code seems to run fine.

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;

//---------------------------------------------------------------------
// this is the most basic class, which everything below it includes the name, color and speed.
class Shape {

    protected:

        char *name;
        char *color;

        double shape_speed;

    public:
        Shape(const char *shapec, double speed){

            char nametmp[] = "Located in shape";
            double const pi = 3.14159;

            int length = strlen(shapec);
            color = new char[length +1];
            strcpy(color,shapec);

            length = strlen(nametmp);
            name = new char[length +1];
            strcpy(name,nametmp);

            shape_speed = speed;

        }
        virtual void print() {
            cout << "name: " << name << endl;
            cout << "color: " << color << endl;
            cout << "speed of shape: " << shape_speed << "\n"<< endl;
        }

        virtual  ~Shape() {
                delete [] name;
                delete [] color;
                cout << "shape deconstructor called" << endl;
        }
};


// This is the class for the three dimensional shapes. this includes the coordinates for the center which are inputed in main
class Shape3d: public Shape {

    protected:

        double x3dc; // center for 3d x coordinate
        double y3dc; // center for 3d y coordinate
        double z3dc; // center for 3d z coordinate

    public:
        Shape3d(const char *shapec, double speed,double x, double y, double z):Shape(shapec, speed){

            Shape(shapec, speed);
            x3dc = x;
            y3dc = y;
            z3dc = z;

            char nametmp[] = "Located in 3d shape";

            int length = strlen(nametmp);
            name = new char[length +1];
            strcpy(name,nametmp);

        }
        virtual void print() {
            cout << "name: " << name << endl;
            cout << "color: " << color << endl;
            cout << "speed of 3d shape: " << shape_speed << endl;
            cout << "center of 3d shape: (" << x3dc << "," << y3dc<< "," << z3dc << ")" << "\n"<<endl;
        }

        virtual  ~Shape3d() {
            delete [] name;
            delete [] color;
            cout << "3d shape deconstructor called" << endl;
        }

};

// This is the class for the three dimensional tetrahedron. This adds the volume of the shape.
class Shape3dtet: public Shape3d {

    protected:

        double edge;
        double volume;
        double calculate_volume() {
            volume = pow(edge,3.0)/sqrt(72.0);
            return volume;
        }
    public:

        Shape3dtet(const char *shapec, double speed,double x, double y, double z, double e):Shape3d(shapec, speed,x,y,z){

            char nametmp[] = "Located in 3d tetrahedron";

            int length = strlen(nametmp);
            name = new char[length +1];
            strcpy(name,nametmp);

            edge = e;
            calculate_volume();
        }

        virtual void print() {
            cout << "name: " << name << endl;
            cout << "color: " << color << endl;
            cout << "speed of tetrahedron: " << ": "<< shape_speed << endl;
            cout << "volume of tetrahedron: " << volume << endl;
            cout << "center of tetrahedron: (" << x3dc << "," << y3dc<< "," << z3dc << ")" << "\n"<<endl;
        }

        virtual  ~Shape3dtet() {
            delete [] name;
            delete [] color;
            cout << "3d tetrahedron deconstructor called" << endl;
        }

};

int main () {

    Shape basic("blue", 5.3);

    Shape3d threedim("smaragdine",22.8,3,3,3 );

    Shape3dtet tet("carnation pink",1,2,3,4,5);


    basic.print();

    threedim.print();

    tet.print();

    return 0;
}

1 Answers1

2

You are calling

delete [] name;
delete [] color;

in the base class destructor as well as the destructors of ALL the derived classes. That leads to double deletion error.

You definitely need to remove those calls from the destructors of the derived classes. In addition, you need to follow The Rule of Three and implement proper copy constructors and copy assignment operators. Otherwise, you will run into the same problem pretty soon.

You can also simplify your code a lot by using std::string instead of char*. In that case, you won't need to worry about The Rule of Three.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270