0

I have a class called Rectangle which holds two pointers to objects of the class Point2D.

class Rectangle: public GeoObjekt
{
private:
    Point2D* lu;
    Point2D* ro;
public:
    Rectangle(Point2D lu, Point2D ro);
}

Rectangle::Rectangle(Point2D lu, Point2D ro) {
    this->lu = &lu;
    this->ro = &ro;
}

To create an object of this class, I call the following line:

Rectangle rectangle(Point2D(0, 0), Point2D(2, 1));

The constructor is working fine, but when I try to access Point2D* lu; or Point2D* ro;, I get an access violation exception:

Exception thrown at 0xFEDC8589 in Rectangle.exe: 0xC00005: Access violation while executing at position 0xFEDC8589.

I checked it with the debugger and the values inside Point2D* lu; or Point2D* ro; are completely different from the values they initially had. They change after leaving the constructor.

Can someone tell me what I am doing wrong?

Note: The line Rectangle rectangle(Point2D(0, 0), Point2D(2, 1)); should remain as it is.

4
  • 4
    Do you understand what a dangling pointer is? Commented Jun 7, 2021 at 18:06
  • 2
    "... have a class called rectangle which holds to pointers..." hold Point2D instead. Commented Jun 7, 2021 at 18:12
  • 2
    Quick tip: use pointers as little as possible in modern C++. Never use operator new except in placement new expressions and always prefer smart pointers and containers such as std::vector and std::array to raw pointers and C arrays. Do this constantly and you will notice your programs will basically never crash, unless you do something really bad. Nowadays in C++17 and C++20 I mostly use pointers as a replacement for std::optional<T&> (which is not legal). Commented Jun 7, 2021 at 18:59
  • @mcilloni ok. Thank you for the hint! Commented Jun 7, 2021 at 20:21

1 Answer 1

3

Change the members lu and ro to be Point2D instead of Point2D*.

The Point2Ds you supplied in the constructor are destroyed on the next line, so your pointers end up pointing at nothing (dangling pointers).

Sign up to request clarification or add additional context in comments.

5 Comments

This worked. I am a bit confused why this is a dangling pointer but I will research for it. Thank you!
@slow You are passing temporary objects to the constructor, and then saving pointers to the objects. The objects are destroyed automatically after the constructor exits, thus leaving the pointers pointing at freed memory. That is why the pointers are dangling. What Camwin suggests is to get rid of the pointers altogether, thus having the constructor make copies of the temporary objects instead.
Another reason for this change: you really do want to store the corners themselves (coordinates), not just where they happen to be located in memory (pointers).
@RemyLebeau I thought only the access will be 'deleted' but the object themselve will still exist. Thank you very much!
@slow you should read up about Lifetime and Scope. In the statement Rectangle rectangle(Point2D(0, 0), Point2D(2, 1)); the two Point2D objects go out of scope, ending their lifetimes and thus are destroyed, when the statement that is calling rectangle() is finished (ie, when ; is reached).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.