(*) As far as I know the Standard allows an implementation to modify the operand of the delete
operator, however most implementations do not do that.
int* ptr = new int(0);
delete ptr; //delete is allowed to modify ptr, for example set it to 0
std::cout << ptr; // UB?
Acknowledging (*), is the reading of ptr
(in the form of printing it) well-defined?
If delete
does modify ptr
, is it allowed to set a trap value, which would make reading ptr
UB?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…