On this page, it's written 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.
Adding a number to a pointer shifts it forward in memory by those many number of sizeof(*p)
units.
So, what is the difference between delete p
and delete p+1
, and why would making the pointer 0
only be a problem with delete p+1
?
You can't do p + 1 = 0
. For the same reason, if you do delete p + 1
then delete cannot zero out its operand (p+1), which is what the question on Stroustrup's FAQ is about.
The likelihood that you'd ever write delete p+1
in a program is quite low, but that's beside the point...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With