Notes about delete:
After the delete operation, we recommend that you reset the pointer value. For example:
Delete P;
After the execution, p becomes undefined. On many machines, although P is meaningless, it still stores the address of the object it points to, and the memory that P points to has been released, because p is not valid.
After the pointer is deleted, the pointer becomes a suspension pointer. The suspension Pointer Points to the memory that was previously stored, but the object has been deleted.ProgramSome imperceptible errors.
Therefore, once the pointer is deleted, it must be set to 0.
Error-prone points in dynamic memory management:
1. Failed to delete the pointer pointing to the dynamic memory. Therefore, the block of memory cannot be returned to the free storage zone. Failed to delete the dynamic memory is called "Memory leakage ". Memory leakage is hard to find. Generally, it will be revealed only after the application runs out of memory for a period of time. Therefore, when executing the delete operation, you can determine whether the deletion is successful.
2. Read and Write the deleted object. If you delete the object pointed to by the pointer and set the pointer to 0, this error is easy to detect.
3. Use two Delete expressions for the same memory space. When two pointers point to the same dynamically created object, an error will occur during deletion, if you perform the delete operation on one of the pointers, return the memory space of the object to the free storage area, and then delete the second pointer. At this moment, the free storage area may be damaged.