Chapter 5 Expressions

5.11 -- The new and delete Expressions

When we write

delete p;

p becomes undefined. Although p is undefined, on many machines,p still contains the address of the object to which it pointed. However, the memory to which p points was freed, so p is no longer valid.

After deleting a pointer, the pointer becomes what is referred to as a dangling pointer. A dangling pointer is one that refers to memory that once held an object but does so no longer. A dangling pointer can be the source of program errors that are difficult to detect.

Best Practices:  Setting the pointer to 0 after the object it refers to has been deleted makes it clear that the pointer points to no object.



你可能感兴趣的:(object,delete)