|
Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006
Auto_ptr with clearPointer-reactor (heap) the removal of two small objects skills Auto_ptr with clearPointer-reactor (heap) the removal of two small objects skills
Procedures for the use of dynamic memory allocation is almost inevitable. If the memory allocation is not "delete" or "delete" and then used against targets in the region, is likely to lead to the collapse or procedural errors. So, once the "new" one object, the code needed to ensure two things :
Remember the "delete" the object;
Home zero corresponding to the target;
These two things seem simple fact : not as complicated procedure, especially in light of the large number of procedures can easily be overlooked anomalies are, in fact, very easy to miss some coding guidelines. Therefore, we must use C + + skills to help themselves : encapsulated in the object, the object's destructor function to delete Reset to achieve the target. Similarly, this technique appears to be realized than the slightly more complicated. But you need not trouble to achieve it has provided a C + + STL auto_ptr (application specific reference manual STL). Packaging Auto_ptr only targeted at a single target; If dynamic allocation guidelines at the target array, we can achieve a auto_array modeled auto_ptr (http : //www.cublog.cn/u/13684/showart.php?id=72593).
In my experience, most cases auto_ptr can satisfy our requirements. It is sometimes difficult to use, for example auto_ptr STL containers do not meet the requirements of elements. It all depends on who can hand this time programming to "delete sources p = 0."
Some say eXtreme Programming code shall hate all plots. I do not limit programming, but I do not like plots code, wrote the following function to combine the two sentences into a function above.
Template "typename T〉 such
Inline void clearPointer (nematic &p)
{
Delete sources
P = 0
} |
| |