我刚开始有点困惑,当我通过函数尝试删除一个新分配的变量时,为什么打印出来的是相同的内存地址,我猜测没有泄漏内存或指针悬挂。
打印出来的是相同的内存地址。
#include <iostream>
using namespace std;
void deallocater(int *p)
{
delete p;
p = nullptr; // memory deleted and no dangling pointer right?
}
int main()
{
int *x = new int(1);
cout<<x;
deallocater(x);
cout<<endl<<x; // why is the same memory address being printed?
return 0;
}
我假设这个函数已经成功运行