1 Pointer Usage Cases Using Pointers
Under normal circumstances, the pointer is lower than the usual declaration variables, especially the speed of New and Delete are quite slow, especially the speed of New and Delete, which generally do not use the pointer variables in NEW and DELETE. The following is a few examples of COODBLIB optimization:
In the DEBUG version, you need to spend: 563.414ms Complete 300032 COBJECT :: Operator new () Operation 469.955ms Complete 140000 DELETE () operations and fully optimized, doing this operation 6 ~ 7 Second, see the pointer space application and destruction may take up a lot of time.
However, because of the operation pointers such as polymorphism, reference, variable exchange, the pointer will have to be used in such cases; at this time, pay special attention to security issues and memory recovery.
2 Pointers safe to use Using Pointers Safely
The above three is the point of use of pointers.
initialization
Benefits should be initialized to null, such as CRecordset * precordset = NULL; in the class's constructor, all member pointer variables should be initialized.
Check State and Parameters
Check function incoming parameters, such as Bool SetName (LPSZNAME == NULL) Return False; ...} Check if the pointer variable of the class is available in the class member function, that is, the pointer variable used for external service is It has been in place. Checking the pointer in memory when the pointer is empty, fit the following reset pointer to prevent multiple deletions from deleting the same pointer memory.
An exception is that the COLLECTION family of VCs (Array / List / MAP) is not required when used because its Getat () function has been judged.
Reset pointer
After deleting the pointer memory, the pointer should be reset to 0, otherwise it may be wrong when the next check pointer is empty. Such as:
DELETE M_PRECORDSET; M_PRECORDSET = NULL;
However, it can be reset before being separated from the scope, such as deleting a temporary pointer variable at the end of the function and deleting member pointer variables in the destroyer.
3 pointer memory recycling freeing memory of pointers
Inner pointer
If this pointer is a temporary variable or although it is a class variable, it will only be used in this function. When the function exits, if the function exits is complex, use the function destroyer to complete, otherwise it is very likely to haul Delete. Examples of the reference function structure specification.
Subject to the destruction of the pointer is the danger and difficult to maintain, such as:
Void main () {char * sz = new char [8]; MEMSET (SZ, 0, 8); Memcpy (SZ, "Name", 4); PrintFName (SZ); // ...}
Void PrintName (Char * SZ) {Printf ("% s", sz); delete [] sz;}
When the two functions above are not the same programmer maintenance, it is easy to remove problems such as memory leaks, repeatly delete memory.
The key points for recovering memory in the function is: Whoever applied for memory.
Class member pointer
The point of deleting the memory of the class is "Who's pointer is reclaimed", not "who is applying for memory". " Therefore, the class is only responsible for recycling its own pointer, regardless of its parent class and support (CCUSTOMER for cGROUP), the pointer memory is reclaimed. This makes the memory recovery of the class simpler, that is, just what member pointer variables are needed for the current class.
The memory recovery of the class member pointer variable is generally performed in the destroyer, and in a small amount, it is necessary to delete the memory before destruction, and the FreeMEMORY () function can be created, but this function is also called in the destroyer. Note that freeMemoryAll () and destruction requirements are generally different, FreeMemoryAll () is a subset of destroyers. CoodbQuery is a typical example. However, it is more complicated to each member pointer variable, there are several cases:
1 The pointer is the address of the external service. This pointer does not need to be deleted, and you can see in the VC CRecordset application:
CDATABASE DATABASE; CRecordset * PRecordset = New CRecordset (& Database);
It can be seen that there must be a pointer in this class to store & database, but this pointer does not need to delete the memory.
In order to clearly discover such a pointer, it is recommended to use m_CP as its prefix, indicating this as a const Pointer.
2 The pointer is internal storage data, you need to delete it. There are two internally stored data, one is a normal pointer, a function (such as a constructor) applied for memory; another is an element of PTRARRAY / LIST / MAP, an external application of the class, is added through the ADD function, However, it is also recycled in the desefer of the class. E.g:
template
It can be seen that the principle of "Who is reclaimed", although m_classarray.getat (i) (actually a pointer to the class from CoodbRecord, if the ccustomer) may have a pointer variable, but its recycling has been It is done with the Class destroyer. The advantage of this package is whether or not Class's internal structure is changed, as long as the memory recovery work is done in the Class destroyer.
Global pointer, static pointer
Since the global pointer is first a global variable, it is generally automatically released as the program ends, but in order to defocize other memory, other memory recycling, its location Perform in the exitInstance () function.
Most global pointers are static pointers, so there is no special requirements in naming, using G_P as a prefix.