getting problem pointer being freed was not allocated when don't use return 0 in main function cpp

Viewed 50
//Example 1 without return 0 in the main function

class B{
  public:
    int *ptr;
    B(){
      this->ptr = new int;
    }
    ~B(){
      delete this->ptr;
    }
    B(const B &b)
    {
      *this = b;
    }
    B & operator=(const B &b)
    {
        delete this->ptr;
        this->ptr = new int;
        return *this;
    }
};

int main()
{
  B x;
  B y(x);
}

in the example 1 i got error pointer being freed was not allocated

//Example 2 with return 0 in the main function

class B{
  public:
    int *ptr;
    B(){
      this->ptr = new int;
    }
    ~B(){
      delete this->ptr;
    }
    B(const B &b)
    {
      *this = b;
    }
    B & operator=(const B &b)
    {
        delete this->ptr;
        this->ptr = new int;
        return *this;
    }
};

int main()
{
  B x;
  B y(x);
  return (0);
}

in the example 2 I don't got the error i tried to print address of this->ptr in the example 1 it hold random address and in the example 2 it hold 0x0 it refer to null and delete ignore null pointer so that why the problem don't happen but the question is why return 0 can make this different i know that return 0 mean process end with success status can any one explain why ? or any doc i can read

0 Answers
Related