C ++ destructor: when is memory freed?

If I delete an object that calls its destructor, will memory be freed before or after the destructor has finished doing everything the function has?

+5
source share
6 answers

Memory is freed only after the destruction of a subobject of the least derived class. Therefore, if you have:

class Base {
};

class Derived : public Base {
public:
    ~Derived();
};

it is first Deriveddestroyed, then Basedestroyed, and only then is memory freed.

+7
source

Fold it deleteinto what it actually does, and see relatively clearly when the memory is deleted. So, an expression like this:

delete some_ptr;

It is roughly equivalent to this pseudo code:

some_ptr->~some_ptr();
free( some_ptr );

, . , , delete, . , .

, . delete .


, free operator delete(), , . .

+3

. - segfaults.

+2

delete ,

+2

, , . , , , .

0

++ - , . .

- , delete , .

Allocator :

  • allocate deallocate
  • construct destroy

, construct, destroy deallocate , . , destroy deallocate .

, , .

0

All Articles