关于 this page , 是这么写的
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.
将一个数字添加到一个指针会在内存中将它向前移动那么多 sizeof(*p)
单位。
那么,delete p
和 delete p+1
有什么区别,为什么指针 0
只是一个问题使用 delete p+1
?
最佳答案
你不能做 p + 1 = 0
。出于同样的原因,如果您执行 delete p + 1
,则 delete 无法将其操作数 (p+1) 归零,这就是 Stroustrup 常见问题解答中的问题。
您在程序中写过 delete p+1
的可能性非常低,但这不是重点...
关于c++ - 为什么我可以做 "delete p;",而不是 "delete (p+1);"?为什么 delete 需要左值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2656970/