-3

I wrote this and wondering why "deleted" isn't showing as output.

int *p=NULL;
p=new int(10);
cout<<*p<<endl;
delete p;
if(p==NULL)cout<<"deleted"<<endl;

Can Someone explain why it isn't printing after using delete and why delete isn't making the pointer NULL ?

sabertooth
  • 582
  • 1
  • 7
  • 23
  • 2
    Have you tried reading the documentation of `delete` – M.M Jul 20 '18 at 05:42
  • 1
    That's not what `delete` does. – Some programmer dude Jul 20 '18 at 05:44
  • 1
    1) There are no references in the code shown. You are `delete`ing a pointer, not a reference. 2) `delete` doesn't change the value of `p`. It just `delete`s the memory pointed at by `p`. – Algirdas Preidžius Jul 20 '18 at 05:46
  • 1
    See [delete expression](https://en.cppreference.com/w/cpp/language/delete) and attempt to find where the pointer is set `NULL` after the call. – David C. Rankin Jul 20 '18 at 05:49
  • Possible duplicate of [Pointers in c++ after delete](https://stackoverflow.com/questions/44182049/pointers-in-c-after-delete) – UnholySheep Jul 20 '18 at 06:43
  • 2
    delete doesn't set the pointer to NULL because doing so would be a waste of CPU cycles in cases where the pointer was never going to be read again anyway. If you want the pointer to be set to NULL, you can explicitly set it to NULL yourself, but since in many cases you won't care, C++ doesn't force you to pay the overhead for an operation you don't want. – Jeremy Friesner Jul 20 '18 at 07:04
  • If you want `p` to be nullptr, then after `delete p;`, add this line: `p = nullptr;`. – Eljay Jul 20 '18 at 11:33

2 Answers2

1

delete works on pointer values not pointer variables. For instance this is perfectly legal

int* some_func();

delete some_func();

As you can see there is no variable here and nothing to set to NULL.

john
  • 85,011
  • 4
  • 57
  • 81
1

It's because when you say delete p you're deleting a pointer to memory which completely erases the reference to the new memory you allocated. When you say if p==NULL you're checking to see if the pointer was set to null when in fact the memory that it was pointing to was de-allocated so the pointer isn't pointing to anything. Which doesn't mean the same as having it point to NULL in C++.

codehelp4
  • 132
  • 1
  • 2
  • 15
  • thanks.. but how to check if a pointer is pointing to any memory – sabertooth Jul 20 '18 at 08:47
  • check if the pointer `p != NULL;` and if it's `true`, the pointer is pointing to some memory – The Apache Jul 20 '18 at 09:29
  • @ToufiqueImam To check to see if the pointer is pointing to anything try to output the value. cout << "output test pointing too..... " << *p; if the number looks like a weird long number that means it's pointing to garbage. – codehelp4 Jul 20 '18 at 21:26
  • So there is no const value like NULL or expression to compare the pointer if it's pointing to any memory? – sabertooth Jul 21 '18 at 16:10
  • @ToufiqueImam Not that I am aware of. C++ is different than languages like Java where arrays for example get a default value of 0 or null depending on the datatype. Can you elaborate more on why you need to check for this so I can assist you furher? – codehelp4 Jul 21 '18 at 21:04
  • I was implementing BST then when performing delete i didnt set the pointer to NULL That cause creation of garbage value during accessing items. Anyway I used delete and set the pointer to NULL in the delete_() after facing it – sabertooth Jul 23 '18 at 00:31
  • yep when when a pointer isn't set to an actual value it points to garbage. Good job fixing it by setting it to null. :) – codehelp4 Jul 24 '18 at 21:12