考虑以下代码:
int a = 10;
int * b = &a;
int * c = b;
delete b; // equivalent to delete c;
Am I correct to understand in the last line, delete b
and delete c
are equivalent, and that both will free the memory space holding a
, thus a
不再访问?
原文由 space_voyager 发布,翻译遵循 CC BY-SA 4.0 许可协议
你的程序的行为是 未定义 的。您 只能 在指向使用
delete
分配的内存的指针上使用new
。如果你写过那么你 可以 写
delete b;
或delete c;
来 释放你的内存。不要尝试 取消引用b
或c
在delete
调用之后,这样做的行为也 _未定义_。