Home > Back-end >  Why the program is over has been caught in the destructor call death cycle?
Why the program is over has been caught in the destructor call death cycle?

Time:09-27

#include
Class a
{
Public:
~ (a)
{
/* the destructor, if there is no custom, then the system automatically generate a destructor */
Printf (" destructing! \n");
Delete this.
}
};

Int main ()
{
A, b.
}

CodePudding user response:

Why are you inside the destructor call delete this??????

CodePudding user response:

You play so recursively until the program crashes,

CodePudding user response:

reference 1st floor ggglivw response:
why are you inside the destructor call to delete this??????

The destructor is not to completely release an object at the end of the life span of memory? If I use the new generate an object, so I hope it done in program calls released when this object is the memory of memory does not automatically release (heap), so I use the delete,

CodePudding user response:

Delete this redundant code
Invoke the delete this enters again a destructor, destructor continue to delete this infinite loop was thus on


New is to delete, no new code, no delete?

The main function A, A. Is a distribution to the inside, stack of automatically released, without the need for a separate write new
Even if A * pA=new A; So also delete pA; Rather than in the destructor to delete this
Destructors to delete is A new internal structure or other means out of memory, this class have no you, so don't delete what




CodePudding user response:

It is best not to study fragmentation, can system to learn, otherwise it is elephant

CodePudding user response:

You didn't understand the new delete usage, it must say the use to match, you are not new, why delete.

CodePudding user response:

Fun
reference 4 floor response:
delete this redundant code
Invoke the delete this enters again a destructor, destructor continue to delete this infinite loop was thus on


New is to delete, no new code, no delete?

The main function A, A. Is a distribution to the inside, stack of automatically released, without the need for a separate write new
Even if A * pA=new A; So also delete pA; Rather than in the destructor to delete this
Destructors to delete is A new internal structure or other means of out of memory, this class have no you, so don't delete what

Oh oh, I see the

CodePudding user response:

refer to fifth floor truth is right or wrong response:
it is best not to study fragmentation, can system to learn, or is the elephant

Ok, I see, thank you

CodePudding user response:

refer to 6th floor ggglivw response:
means that you didn't understand the new delete usage, it must say the use to match, you are not new, why delete.

Use to match? But why?

CodePudding user response:

Want to get a piece of code executed, must want to have a thread to execute,
You think of it, since you are the main function of entrance, your constructor and destructor, which is when a thread to perform?

CodePudding user response:

references 9 f weixin_46024012 response:
Quote: refer to the sixth floor ggglivw response:
means that you didn't understand the new delete usage, it must say the use to match, you are not new, why delete.

Use to match? But why


C + + is the logic of your allocated memory should you delete yourself, GO and JAVA logic is that you allocate memory, if there is no object to it, the interpreter will be in a time release yourself, don't you manually, use what language should follow the specification of the he.
  • Related