explicit call to destructor is not destroying my object why?

前端 未结 7 531
我寻月下人不归
我寻月下人不归 2020-12-03 08:56

I\'m calling the destructor to deallocate memory but it is not deleting my object. What is the reason behind it?

my code is like this:

class A
{
publ         


        
7条回答
  •  臣服心动
    2020-12-03 09:55

    What you're doing is actually invoking undefined behavior ... just because you've called the destructor, does not mean that the memory is zeroed out or necessarily "reclaimed" and inaccessable (especially in the case of an automatic variable that was allocated on the stack and not the heap). It could be, but that is left up to the implementation, and typically that is not done due to performance reasons, which is typically the reason for using C++ in the first place. Therefore you can theoretically access the values at the memory address that the object was occupying after calling the destructor ... but again, it's undefined behavior, and you can run into pretty much anything from a segmentation fault, to a silent error that corrupts memory somewhere else, etc.

提交回复
热议问题