The semantics of execution, that is, when a program executes, the compiler produces an instruction call that guarantees the construction of the object, the release of the memory, and the security of the type conversion and the generation of the temporary object.
I. Construction and deconstruction of objects
For the construction of class objects, we should try to define them as much as we need to use them, and then start the internal construction process after the definition.
For a constructed object, the destructor must be placed before each departure point (when object is still present).
second, the global objectFor global variables, C + + guarantees that global variables are constructed before global variables are invoked. All global variables are placed in the program's data segment (datasegment) and are initialized to 0 for variables that do not display the specified value. At the same time, the global class object is placed in the datasegment at compile time and the content is 0. The corresponding constructor is only implemented when the program is started. Soobject requires static initialization。 Statically initialized Munch policy: Generates a _sti () function for each file that needs to be statically initialized, containing the necessary constructor invocation operations and Inlineexpansions. Generates a _STD () function for each file that requires a static memory release operation. Provides a _main () function that calls all the _sti () functions, and a _exit () function calls all _STD () functions
third, local static objectsThe constructor of a local static object can only be performed once, and the destructor is only once. So for local static objects, a very simple idea is to import a temporary object, when the first time passed, set to true, then no longer processing. Conditional destructors are also necessary for all compilers, and staticlocalclassobject need to be refactored in the opposite order of construction.
four, Object arrayFor the following array definition: POINTKNOTS[10];We typically use the vec_new () function to construct the array (the way it is in Cfront), and for VS, it provides a class to process without "Virtualbaseclass", and the other is used to process "contained Virtualbaseclass" 's class. The latter function is often called vec_vnew (). The prototype of a function is generally as follows: void* vec_new (void*array,//array's starting address size_telem_size,//each classobject size intelem_count,//the number of elements in the array void (*constructor) ( void*)//class constructor function pointer void (*destructor) (Void*,char)//class destructor function pointerSo for the initialization of the point object above, the corresponding vec_new () is instantiated as follows: Vec_new (&knots,sizeof (point), 10,&point::P oint,0);Similarly, for deletion of objects, there are similar vec_delete () to operate. void* vec_new (void*array,//array's starting address size_telem_size,//each classobject size intelem_count,//the number of elements in the array void (*destructor) ( Void*,char)//class destructor function pointers
Five, new, and delete operators Int*pi=newint (5);In fact, it's done in two steps, the first step is to configure the required memory with the appropriate new operator function instance, and then set the initial value of the configured memory. The difference between the use of pi and the object that Pi refers to is which life is over. Because even if the object is not legitimate, the object that the pointer refers to is legitimate. New is actually done through standard Cmalloc, each call to new must return a unique pointer to the default 1byte memory address. If the class object array does not have constructor and destructor defined, then vec_new is not invoked. For Delete[]p_array, the compiler looks for the dimensions of an array only if the brackets appear, otherwise he only assumes that a single object is to be deleted. So how do you record the number of elements in an array? An obvious way to do this is to set an extra word for each memory area block that vec_new () returns, and then wrap the number of elements in that word. And the value of the parcel is usually called a cookie. In the original compiler, there are two main functions used to access cookies
Notice that a baseclass pointer is prevented from pointing to an array consisting of a derivedclassobject. That POINT*PTR=NEWPOINT3D[10];If you have to do this, by default it will only be handed to the "deleted pointer type destructor" of the execution Vec_delete () function, which is pointdestructor. Therefore, the programmer needs to release the memory as significant as the following. for (Intix=0;ix<elem_count;++ix) {point3d*p=& (point3d*) PTR) [IX]; deletep;}
Vi. about Placementoperatornew This is a predefined overloaded new operator, which is invoked as follows: Point2w*ptw=new (Arena) point2wWhere Arena is a block of memory that is used to place the resulting point2wobject. One of the benefits of this is that the other half of Placementnewoperator's expansion is point2wconstructor automatically on arena: point2w*ptw= (point2w*) Arena; if (ptw!=0) ptw->point2w::P oint2w (); so the benefit of placementnewoperator is to decide where object is placed and execute it here constructor。 #include <iostream> #include <typeinfo> usingnamespacestd; classpoint{Public:point (Intx,inty): _x (x), _y (y) {}; Voidtest () {cout<< "Helloword:" <<_x<<_y< <endl;} ~point () {cout<< "destructor" <<endl;}; protected:int_x,_y; }; Intmain () {char*arena=newchar[sizeof (point)]; Point*pt=new (Arena) point (1,3); Pt->test (); deletept; Pt->~point (); Pt=new (Arena) point (2,3); Pt->test (); cout<< (pt!=nullptr) <<endl; Return0;
The correct method for placement: p2w->~point2w; p2w=new (Arena) point2w;
vii. Temporary Objects The destruction of temporary objects should be the last step in the evaluation of a complete expression. The complete expression causes a temporary object to occur. A temporary object holding the result of an expression execution should be preserved until the initialization of object is complete. If a temporary object is bound to a reference, the object remains until the initialization reference the end of life, or until what category of the temporary object ends.