所以,这是我解释这个问题的例子
void * p1;
int * p2, * p3;
p2 = new int;
p1 = p2;
p3 = (int *) p1;
要释放内存,以下 3 行是否彼此等效?
delete p2;
delete p3;
delete (int *) p1;
我使用它的原因是我想在函数之间传递一个指针而不知道它的类型,例如我定义一个 void 指针并通过调用其他函数来更改它的值,如下所示:
void * p1;
func1(p1); //in this function, p2 = new int and p1 is assigned as p1 = p2;
func2(p1); //in this function, p1 is assigned to another pointer: int * p3 = (int *)p1;
然后,我调用了 func3 来释放内存
func3(p1); //delete int * p1
调用 func3 后,我是否必须再处理 func1 中的 p2 ?
谢谢!