Essay sample library > To store a destructor

To store a destructor

2023-06-30 02:53:21

After Friday 's presentation, some people asked me how I would save the destructor to my gcpp library. I was interested in a few people, so I thought that I would write a note.

The simple answer is to store two raw pointers. One points to an object, one destructor points to type erasure, and it easily writes it in lambda.

Therefore, it stores two raw pointers, the original pointer to the object to destroy and the original function pointer to the destructor. Then we call it d.destroy (d.p).

The most common problem is how to get a pointer to the destructor's original function. I will use lambda.

Since we know that we only describe lambda to execute the correct cast in order to call the correct T destructor, we can use it as a normal function because lambdas that do not capture have no state. Therefore, for each different type T instantiated, we generate a T-specific lambda function (on-demand, globally unique at compile time) and store the address of that function.

In this case, λ provides a convenient type of erasing method. One row deletes the type and the other row deletes the type

Yes, it is legal to call the destructor on the const object. Must be able to destroy the const object. Const never applies to constructors or destructors. It works for all other member functions.

Some people already asked what the body of the rapper lambda is. Several weeks ago, I looked at the code gen of Clang. Depending on the level of optimization, lambda was generated as an instruction function (only one jmp for the actual destructor) or as a copy of the destructor if inline (there is no run-time overhead at all, destructors in the binary Another inline copy (usually inline)

Since we store the destructor during construction of the T object we can see that the complete object is T type. Therefore it is easy to store the correct destructor for a complete object. After that, the correct T destructor is called to clean up the whole object whether or not deferred_ptr holds anything such as the active list deferred_ptr, or deferred_ptr, deferred_ptr, deferred_ptr etc.

Destructors are also special member functions used in the C ++ programming language. The destructor has the opposite function to the constructor. The main purpose of the destructor is to release dynamically allocated memory. Destructors are used to free memory, free resources, and perform other cleanups. When the object is destroyed, the destructor is automatically named. Like the constructor, use the same name as the class name for the destructor. Member functions can be used (and must also be used) to interact with data contained in user-defined types. User-defined types provide flexibility for "divide-and-conquer" approach in programming. In other words, the programmer can write a user-defined type and guarantee the interface. Another programmer can write the main program using the expected interface. These two parts are gathered and compiled for use. User - defined types provide encapsulation defined in the object - oriented programming (OOP) paradigm.

Since we store the destructor during construction of the T object we can see that the complete object is T type. Therefore it is easy to store the correct destructor for a complete object. Later, deferred_ptr <T> is called to clean up the complete object, such as deferred_ptr <const T>, deferred_ptr <BaseClass>, deferred_ptr <TDataMember>, regardless of whether the deferred_ptr list is active .

The destructor method can be defined in any C # class, but the problem with destructors (also known as finalizers in C #) is that you can not determine when to call. They are called by the garbage collector on future uncertainties (called by another thread and other problems may occur). It is not a best practice for C # to try to force garbage collection by using GC.Collect () to resolve these limitations. Block threads for unknown time while collecting all objects that meet the collection criteria.