c++ - Two pointers not deleted properly -
so i'm trying code out, compiling in cl (visual studio c++ compiler) , keeps printing 0. shouldn't y equal nullptr?
#include <iostream> #include <string> using namespace std; int main() { int* x; x = new int(5); int* y; y = x; delete x; x = nullptr; cout <<(y==nullptr)<< endl; return 0; }
no, setting x nullptr not set y nullptr too.
y int* not reference int*.
so, y == nullptr necessarily1 false.
1 pub quiz: x cannot nullptr since if allocation failed std::bad_alloc have been thrown. would possible y nullptr had written x = new(std::nothrow) int(5);
Comments
Post a Comment