#include <iostream>
using namespace std;
int main()
{
int* ptr = new int(7);
cout << *ptr << endl;
delete ptr;
ptr = nullptr;
if (ptr != nullptr)
{
cout << ptr << endl;
cout << *ptr << endl;
}
return 0;
}
delete ptr;
과 ptr = nullptr;
은 같이 다닌다. 왜냐하면 할당한 메모리를 해제하면 ptr은 쓰레기값을 가리키는데, 이를 nullptr로 초기화하면 아무것도 가리키지 않아서 ptr은 0의 주소를 가진다.