Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 995595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:44:50+00:00 2026-05-16T06:44:50+00:00

I am trying to overload the assignment operator to do a deep copy of

  • 0

I am trying to overload the assignment operator to do a deep copy of a polygon object, the program compiles but I am getting an error toward the end that I want to clear up. Below is the relevant code, if you think I need to add more please just post a comment. Assume the proper #include‘s and that the << operator is overloaded for proper output etc…

The error is: malloc: * error for object 0x1001c0: pointer being freed was not allocated
*
set a breakpoint in malloc_error_break to debug.

//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;

...

I have read elsewhere that this is possibly due to a bug in OS 10.6 or Xcode 3.2 if there is a workaround could someone please give me detailed instructions for how to do the workaround, I do not have a lot of experience with Xcode.

Edited: added parts of code that use delete, notice that it is being used in the destructors for Polygon and PolygonNode

Edited: added the part of the code where link_ is allocated, setLink is a simple setter method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:44:51+00:00Added an answer on May 16, 2026 at 6:44 am

    I can’t see the constructor for the PolygonNode class. Is the link_ pointer initialized to null on creation? If not, that may be the problem manifesting itself in the error you get. You have to make sure, the link_ pointers in the PolygonNode instances get initialized to null. Define appropriate constructors.

    Do you have a copy constructor defined for your polygon class? I can’t see one in the code posted, but maybe you just didn’t paste it and you have one. If not, that is one of possible sources of serious problems.

    The copy constructor, that gets synthesized automatically by the compiler will just copy the pointers in the Polygon class.

    Your assignment operator takes the argument by value

    Polygon& operator= (Polygon ply);
    

    This makes use of the copy constructor. If it’s the automatically synthesized one, ply inside the operator has pointers pointing to the same list, the argument passed by value to the operator owns. ply behaves like it owned the list too and the list gets destroyed when ply goes out of scope. The original argument is left with dangling pointers.

    You should define correct copy constructor.

    You should also consider taking the argument in the assignment operator by const reference. I don’t see a reason to take it by value. Maybe you have one, but even if you do, you can change it temporarily, to test the operator, before you define correct copy constructor. In your operator you should check for self-assignment. All I can see now is adding new nodes to the old Polygon. I don’t think it’s right, but I guess it’s just for testing now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.