C ++ primer Reading Notes (2)

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.