-1

I think it has to do with memory allocation issues, but there is a destructor there to free the memory of the object t of type A.

 #include <stdio.h>

struct A {
  int* i;
  A() { i = new int[3]; }
 ~A() { delete i; }
};

int main() {
  A t;
}
Jason
  • 36,170
  • 5
  • 26
  • 60
cb_ann
  • 33
  • 3
  • 1
    See [Rule of Three/Five](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) at minimum. But you've also paired a `new[]` with a non-array `delete`, which is undefined behavior. – Silvio Mayolo Jul 06 '22 at 04:46
  • [Dupe1](https://stackoverflow.com/questions/72197838/program-crashes-when-calling-constructors), [Dupe2](https://stackoverflow.com/questions/72870386/why-the-destructor-is-called-only-one-time-when-the-constructor-is-called-5-time/72870456?noredirect=1#comment128708410_72870456) – Jason Jul 06 '22 at 04:59

1 Answers1

0
~A() { delete i; }

should be

~A() { delete []i; }
pm100
  • 48,078
  • 23
  • 82
  • 145
  • 1
    Could've closed this as a dupe. There are plenty of dupes for this and this question is asked very often. – Jason Jul 06 '22 at 04:58
  • Voting to delete [from review](https://stackoverflow.com/review/low-quality-posts/32182420). – Al.G. Jul 06 '22 at 08:34