glibc在运行C++代码时检测到*双自由或损坏错误
glibc detected * double free or corruption error when running C++ code
我研究了关于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*
,您还可以大大简化代码。在这种情况下,你就不必担心"三条规则"了。
相关文章:
- 创建 OpenCV 非自由版本 v4.3 时出错,可折叠.cpp错误 C2039、2605
- 瓦尔格林德错误 - 地址0x0不是堆叠的 malloc'd 或自由的
- Malloc后的自由功能错误
- 双自由错误而不是分割错误
- 使用GDB修复大型项目中的双自由或损坏(!prev)错误
- C++内存分配/释放和自由空间错误
- c++双自由或损坏(fasttop)--不确定错误在哪里
- OpenCV 编译错误与非自由/feature2d.hpp.
- 当我在c++中擦除向量时,我遇到了双自由错误
- C++glibc双自由错误
- 具有自由功能常量和非常量过载的增强型纠删错误
- 自由图像报告图像颜色类型错误
- 为什么在运算符重载中会出现自由错误
- 为什么会出现这种双自由错误
- C++Glibc检测到错误.双重自由或腐败
- 在自由函数中定义的类型,可通过自动外部访问.语言错误或功能
- glibc在运行C++代码时检测到*双自由或损坏错误
- C++中的双自由错误
- 双重自由错误
- 未使用复制构造函数时出现双自由错误