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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -