0
#include <iostream>
using namespace std;
class Point {
private:
    int x;
    int y;
    static int numCreatedObjects;
public:
    Point() : x(0), y(0) {
        numCreatedObjects++;
    }

    Point(int _x, int _y) : x(_x), y(_y) {}
    ~Point() {
        cout << "Destructed..." << endl;
    }
    void setXY(int _x, int _y) {
    
        this->x = _x;
        this->y = _y;
    }

It seems to compile fine so far. Constructors and destructors are checked until they are fully operational.

    int getX() const { return x; }
    int getY() const { return y; }
    // *this + pt2 -> 
    Point operator+(Point& pt2) {
        Point result(this->x + pt2.getX(), this->y + pt2.getY());
        return result;
    }
    //operator overloading
    Point& operator=(Point& pt) {
        this->x = pt.x;
        this->y = pt.y;
        return *this;
    }
    static int getNumCreatedObject() {
        return numCreatedObjects; }
    friend void print(const Point& pt);
    friend ostream& operator<<(ostream& cout, Point& pt);
    friend class SpyPoint;
};

The hack_all_info fucntion on SpyPoint class must do like that change Point pt x, y to 40, 60. and static int must be changed 10.

so i compiled like this;

//static (numCreatedObjects)
int Point::numCreatedObjects = 0;

void print(const Point& pt) {
    cout << pt.x << ", " << pt.y << endl;
}

ostream& operator<<(ostream& cout, Point& pt) {
    cout << pt.x << ", " << pt.y << endl;
    return cout;

}
class SpyPoint {
public:
    
    void hack_all_info(Point &pt) {
        pt.x = 40;
        pt.y = 60;

        cout << "Hacked by SpyPoint" << endl << "x:" << pt.x << endl << "y: " << pt.y << endl;
        cout << "numCreatedObj.: " << pt.numCreatedObjects << endl;

    }
};
int main() {
    Point pt1(1, 2);
    cout << "pt1 : ";
    print(pt1);
    cout << endl;
    Point* pPt1 = &pt1;
    
    cout << "pt1 : ";
    cout << pPt1->getX() <<", "<< pPt1->getY() << endl;
    cout << "pt1 : ";

    Point* pPt2 = new Point(10, 20);
    cout << endl;
    cout << "pt2 : ";
    cout << pPt2->getX() << ", " << pPt2->getY() << endl;
    cout << endl;
    delete pPt1;
    delete pPt2;
    cout << "pt1 NumCreatedObject : ";
    cout <<  Point::getNumCreatedObject() << endl;

    Point pt2(10, 20);
    Point pt3(30, 40);
    Point pt4 = pt2 + pt3;
    cout << "pt1 NumCreatedObject : ";
    cout << Point::getNumCreatedObject() << endl << endl;
    // object array
    Point* ptAry = new Point[5];
    cout << "pt2 NumCreatedObject : ";
    cout << Point::getNumCreatedObject() << endl;
    cout << endl;

    delete[] ptAry;
    cout << endl;
    // friend class
    SpyPoint spy;
    cout << "pt1 info" << endl;
    spy.hack_all_info(pt1);
    cout << "pt4 info" << endl;
    spy.hack_all_info(pt4);
    return 0;
}

There is no problem compiling from here, but an error occurs on execution.
But I don't know where the pointer is wrong :(

  • 11
    `delete pPt1;` - `pPt1` does **not** point to an object allocated with `new` so you cannot `delete` it! – UnholySheep May 09 '23 at 13:05
  • OMG I missed Thank you!! – Jeongbokja May 09 '23 at 13:06
  • 1
    @Jeongbokja Upvoting the helper is the best way to say thanks! – Yogesh May 09 '23 at 13:09
  • I'm very sorry annoying you, How can I upvote comment?! I can't find arrows on comment - on PC! I only can find flag : – Jeongbokja May 09 '23 at 13:16
  • 2
    @Jeongbokja Why are you using `new` at all? There would be no memory leaks if you didn't write code that would allow them. This: `Point* pPt2 = new Point(10, 20);` could have simply been `Point pPt2(10, 20);`, and `Point* ptAry = new Point[5];` could have been `Point ptAry[5];` or `std::array ptAry;` – PaulMcKenzie May 09 '23 at 13:19
  • 4
    A very important distinction that no one seems to get taught is that compiles fine != no issues. – sweenish May 09 '23 at 13:22
  • what is the error on execution? – user253751 May 09 '23 at 13:24
  • @sweenish Yes, if compiling with no errors means that there are no bugs, then every program that is running right now would be bug free, and you know that isn't the case. – PaulMcKenzie May 09 '23 at 13:26
  • @PaulMcKenzie it is because the C++ textbook's question. I must use Dynamic allocation on these values - pPt2, ptAry. – Jeongbokja May 09 '23 at 13:30
  • @user253751 When I changed to pPt1 → pPt1->~Point(), it was solved. Compiled but had problems with program operation (based on Visual Studio) – Jeongbokja May 09 '23 at 13:32
  • *it is because the C++ textbook's question* -- Then everything is topsy-turvy in how that textbook is presenting pointers to you. You have a `Point` class, and it contains no pointers. You have an `operator +` that contains no pointer usage. All is fine up to the point where you are told to use `new` inside of `main`. That totally goes against good practice. You are not supposed to introduce dynamic allocation when not needed. If anything, I would have expected your classes to use pointers, and in there you have to figure out how to use `new` and `delete`. – PaulMcKenzie May 09 '23 at 13:33
  • 2
    `Point* pPt2 = new Point(10, 20);` -- Also, code that looks like this is an indication of a Java or C# programmer's attempt of writing C++ code without those programmers learning C++ properly. Then it becomes a habit that's hard to break, because they are so used to writing code like this. That's the other bad thing about this -- the code you were told to write is a well known "red flag" that a Java/C#/JS/Python programmer is hacking together a C++ program for the first time. – PaulMcKenzie May 09 '23 at 13:36
  • @PaulMcKenzie Thank you for your detailed feedback! i want to upvote comment, but I can't find where can i upvote. – Jeongbokja May 09 '23 at 13:42
  • @Jeongbokja: the little gray triangle to the left of a comment is the "arrow" you use to up-vote that comment. – Jerry Coffin May 09 '23 at 15:02

1 Answers1

0

You try to delete an object that is on the stack.

When you use new to create an object, it is created on the heap and you have to destruct and free the memory space with delete, when you don't need it anymore.

But here pt1 is created on the stack (without new), so it is stored together with other local variables, and is destructed automatically when you return from the function.

So you have to remove delete pPt1;.

jjj
  • 575
  • 1
  • 3
  • 16
  • Note: Try to avoid the terms heap and stack in general questions, especially a question like this one where the terms Automatic and Dynamic [all-but-eliminate the asker's conceptual problem](https://stackoverflow.com/q/9181782/4581301). – user4581301 May 09 '23 at 19:20