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.
本文探讨了C++中delete操作符使用后的指针状态,即悬空指针的概念。解释了当一个指针指向的对象被释放后,该指针仍然保留原地址但已无效的情况,并讨论了这种情况下可能出现的难以察觉的程序错误。建议在对象被删除后将指针置为0以避免潜在的问题。
1350

被折叠的 条评论
为什么被折叠?



