微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 错误:未分配被释放的指针

我试图重载赋值运算符来执行多边形对象的深层复制,程序编译,但我得到一个错误,我想要清除.以下是相关代码,如果您认为我需要添加更多,请发表评论.假设正确的#include和<<运算符过载以获得正确的输出等... 错误是:malloc:*对象0x1001c0的错误:未分配被释放的指针
*在malloc_error_break中设置断点以进行调试.

//polygon.h
// contains two classes polygonNode and polygon
class polygonNode //Used to link points in a polygon so that they can be iterated through in order
{
public:
...
methods etc
...
private:
Point pt_; // the points in the polygon are made using the Point class
polygonNode* link_ ; // pointer to the next point in the polygon
};

class polygon // Connects points and forms a polygon { public: ... polygon& operator= (polygon ply); void polygon::addPoint(const Point &p); // methods etc ... private: int numPoints_; bool closed_polygon_; polygonNode* first_ ; // points to the first point of the polygon polygonNode* last_ ; // points to the last point of the polygon };

//polygon.cpp
...
polygonNode::~polygonNode()
{
    delete link_ ; // possible problem area
}

polygon::~polygon() { delete first_ ; // possible problem area last_ = NULL ; }

void polygon::addPoint(const Point &p) { polygonNode* ptr ; ptr = new polygonNode(p) ; if( last_ != NULL ) last_->setLink(ptr) ; last_ = ptr ; if( first_ == NULL ) first_ = last_ ; numPoints_++ ; } polygon& polygon::operator= (const polygon ply) { for (int i = 0; i < ply.numPoints()-1; i++) { addPoint(ply.getPoint(i)); } if (ply.isClosed()) { closepolygon(); } else { addPoint(ply.getPoint(ply.numPoints()-1)); } return this; } void polygon::addPoint(const Point &p) { polygonNode ptr ; ptr = new polygonNode(p) ; if( last_ != NULL ) last_->setLink(ptr) ; // sets the last pointer to the new last point last_ = ptr ; if( first_ == NULL ) first_ = last_ ; numPoints_++ ; } ...

//main.cpp
polygon ply;
...
        Point pt0(0,0);
        Point pt1(1,1);

    ply.addPoint(pt0);

    cout << "ply = " << ply << endl;
    polygon newply;

    newply = ply; // use of the assignment operator

    cout << "polygon newply = ply;" << endl;
    cout << "newply = " << newply << endl;
    cout << "ply = " << ply << endl;

    newply.addPoint(pt1);
    cout << "newply.addPoint(Point(0,0)); " << endl;

    cout << "newply = " << newply << endl;
    cout << "ply = " << ply << endl;

...

我在其他地方读到这可能是由于OS 10.6或Xcode 3.2中的一个错误,如果有一个解决方法,有人可以请给我详细说明如何进行解决方法,我没有很多使用Xcode的经验.

编辑:添加了使用删除代码部分,注意它正在多边形和多边形的析构函数中使用

编辑:添加了分配link_的代码部分,setLink是一个简单的setter方法.

解决方法:

我看不到polygonNode类的构造函数. link_指针在创建时是否初始化为null?如果没有,那可能是你得到的错误中出现的问题.您必须确保,polygonNode实例中的link_指针初始化为null.定义适当的构造函数.

您是否为多边形类定义了复制构造函数?我在代码中看不到一个,但也许你只是没有粘贴它而你有一个.如果不是,那可能是严重问题的根源之一.

由编译器自动合成的复制构造函数将只复制polygon类中的指针.

赋值运算符按值获取参数

polygon& operator= (polygon ply);

这使用了复制构造函数.如果它是自动合成的,则运算符内的ply指向同一列表的指针,由值传递给运算符的参数拥有. ply的行为就像它拥有列表一样,当ply超出范围时,列表会被破坏.最初的论点留下了悬空指针.

您应该定义正确的复制构造函数.

您还应该考虑通过const引用在赋值运算符中获取参数.我认为没有理由接受它的价值.也许你有一个,但即使你这样做,你可以在定义正确的复制构造函数之前临时更改它,以测试操作符.在您的运算符中,您应该检查自我分配.我现在能看到的只是向旧的polygon添加新节点.我不认为这是对的,但我想这只是为了现在进行测试.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐