我有一个小作业问题。
给出如下代码:
int * p;
p = new int;
*p = 5;
问题是:为什么做没用
p = 0;
delete p;
但做起来很有用
delete p;
p = 0;
?
在我看来,两者都是无用的。如果一个对象被删除,则不能分配新的值。
在这两种情况下,我都会遇到 Segmentation fault
。
最佳答案
为什么它对以下有用:
delete p;
p = 0;
引用 stroustrup 的回答:Why doesn't delete zero out its operand?
Consider
delete p; // ... delete p;
If the ... part doesn't touch p then the second "delete p;" is a serious error that a C++ implementation cannot effectively protect itself against (without unusual precautions). Since deleting a zero pointer is harmless by definition, a simple solution would be for "delete p;" to do a "p=0;" after it has done whatever else is required.However, C++ doesn't guarantee that. One reason is that the operand of delete need not be an lvalue. Consider:
delete p+1; delete f(x);
Here, the implementation of delete does not have a pointer to which it can assign zero. These examples may be rare, but they do imply that it is not possible to guarantee that
any pointer to a deleted object is 0.'' A simpler way of bypassing that
rule'' is to have two pointers to an object:T* p = new T; T* q = p; delete p; delete q; // ouch!
C++ explicitly allows an implementation of delete to zero out an lvalue operand, and I had hoped that implementations would do that, but that idea doesn't seem to have become popular with implementers. If you consider zeroing out pointers important, consider using a destroy function:
template<class T> inline void destroy(T*& p) { delete p; p = 0; }
Consider this yet-another reason to minimize explicit use of new and delete by relying on standard library containers, handles, etc.
Note that passing the pointer as a reference (to allow the pointer to be zero'd out) has the added benefit of preventing destroy() from being called for an rvalue:
int* f(); int* p; // ... destroy(f()); // error: trying to pass an rvalue by non-const reference destroy(p+1); // error: trying to pass an rvalue by non-const reference
关于c++ - 为删除的对象赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16270511/