glibc在运行C++代码时检测到*双自由或损坏错误



我研究了关于glibc检测到的错误的其他几个问题,但我的代码似乎有点不同。我相信这个错误在我的四面体类中,因为如果我把它和对它的调用一起注释掉,代码似乎运行得很好。

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

您正在调用

delete [] name;
delete [] color;

在基类析构函数以及所有派生类的析构函数中。这会导致双重删除错误。

您肯定需要从派生类的析构函数中删除这些调用。此外,您需要遵循"三条规则",并实现适当的复制构造函数和复制赋值运算符。否则,你很快就会遇到同样的问题。

通过使用std::string而不是char*,您还可以大大简化代码。在这种情况下,你就不必担心"三条规则"了。

最新更新