C++ Pointer after delete it then give value -


i come against problem, create 3 block of memory, i'm confuse if delete *r, **r still exist or not? should move **r *r's position? need "new int" statement give value?

int t = 5; int **r; r = new int *;  //declare pointer   *r = new int; delete *r;      // delete pointer *r = t;         //give new value 

sorry ask question mistake. still learning in it. thanks.

your code correct (ideone):

#include <iostream> using namespace std;  int main() {     int **r = new int *; // declare pointer int*     cout << r << endl;   // outputs address in memory (pointer int*)     cout << *r << endl;  // outputs garbage value //  cout << **r << endl; // it's invalid      *r = new int;        // assign memory pointed r new value     cout << *r << endl;  // outputs address in memory (pointer int)     cout << **r << endl; // outputs garbage value      delete *r;           // delete pointer int     cout << *r << endl;  // outputs same address in memory, can't dereference //  cout << **r << endl; // it's invalid, because deleted *r      // here can acces r, *r, not      return 0; } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -