0

Why pointer not getting set to nullptr after delete?

class A{ public: void foo() { cout << 1; } }; int main() { A* obj = new A; delete obj; obj = nullptr; //obj = NULL obj->foo(); //Output: 1 return 0; } https://code.sololearn.com/c99ntKkoGOPR/?ref=app

5th Jan 2019, 12:52 AM
JanSeliv
JanSeliv - avatar
3 ответов
+ 5
It is, but C++ doesn't care if you don't. C++ doesn't protect you from coding junk. A::foo(nullptr) can be called to output 1 as this wasn't used. Try accessing a property and a zero address would likely crash the program. But, that is OS dependent. It could just as easily work.
5th Jan 2019, 1:56 AM
John Wells
John Wells - avatar
+ 2
Just a record of me asking the same problem a year ago. https://www.sololearn.com/discuss/541715/?ref=app
5th Jan 2019, 1:12 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
@John Wells, you are right, I can not access a property. Thank you Image demonstration: https://drive.google.com/file/d/1dV6gT3w3FhCYANjP0Ub9c3z0QRuEV-Lv
5th Jan 2019, 11:38 AM
JanSeliv
JanSeliv - avatar