【问题标题】:glibc detected * double free or corruption error when running C++ codeglibc 在运行 C++ 代码时检测到 * 双重释放或损坏错误
【发布时间】:2015-11-09 18:49:06
【问题描述】:

我查看了围绕 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;
}

【问题讨论】:

  • 代码太多。请提供一个最小的、完整的可验证示例。
  • 你为什么不用std::string?? new char [ ] 的东西是怎么回事?这又是 1990 年代初期吗?
  • 正如 Anon 所说,请minimal reproducible example

标签: c++ glibc corruption


【解决方案1】:

你在打电话

delete [] name;
delete [] color;

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

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

您还可以通过使用std::string 而不是char* 来大大简化您的代码。在这种情况下,您无需担心The Rule of Three

【讨论】:

    猜你喜欢
    • 2012-06-13
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多