-2

Is it necessary to make pointer NULL when the pointer is no more in use. For example

Foo *foo=new Foo();
//code to be processed by pointer foo

/*end of code*/
delete foo;
foo=NULL;     //the point which i am asking

My questions are:

  • What kind of effects i.e., good or bad it can cause to the program?
  • What will happen if we don't use this?

Thanks in advance for clearing my points.

  • You mean `foo=nullptr;`, not `foo=NULL;` because `NULL` has no sense in C++ – Basile Starynkevitch Feb 11 '20 at 16:32
  • If the pounter is not used any more, there is no reason to make it null. But you should not be using raw pointers anyway. – n. m. could be an AI Feb 11 '20 at 16:34
  • 1
    @n.'pronouns'm. "you should not be using raw pointers anyway" I cannot agree. One should not be using owning raw pointers. There are still use cases for raw pointers and then it is still good practice to have them either valid or null pointer – 463035818_is_not_an_ai Feb 11 '20 at 16:35
  • 2
    @BasileStarynkevitch What? NULL is defined in the C++ standard (it's a stub to the C definition). – NathanOliver Feb 11 '20 at 16:35
  • Its not necessary, but is good practice. Using a deleted pointer is a common bug, and having it caught immediately (due to null dereference) makes catching and debugging it much easier. – Uri Raz Feb 11 '20 at 16:41
  • 1
    @idclev463035818 well Inwould use non-nullable smart pointers by default, see "the billion dollar mistake". – n. m. could be an AI Feb 11 '20 at 16:44
  • @n.'pronouns'm. I heard about it, maybe I should read more (i mean of course I didnt want to advocate usage of raw pointers) – 463035818_is_not_an_ai Feb 11 '20 at 17:02
  • In modern C++, best practice is to not use the `new` and `delete` keywords, since there are higher-level tools for doing almost everything they can do but more simply and more safely. – aschepler Feb 11 '20 at 17:22
  • @aschepler I am using visual studio enterprise 2017 for this. – Ghazanfar Ateeb Feb 11 '20 at 18:02

1 Answers1

2

No. You don't need to assign nullptr to a pointer variable if you are no longer going to use it.

But if you need to detect whether or not the pointer variable refers to a valid object at some later point in the program, then setting it to nullptr after you delete it, and later testing for that is the most simple way to do that.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • 1
    I can't agree with your last sentence. It's a terrible way to do that because it won't somehow magically set other copies of the pointer to `nullptr`. – David Schwartz Feb 11 '20 at 16:48
  • @David of course it will not. I never claimed it would. But it will let you test *this* variable. I never claimed it would let you do anything else. Feel free to add your own answer :) – Jesper Juhl Feb 11 '20 at 16:52
  • 1
    It's not entirely clear what you mean by "detect whether or not the pointer variable is valid at some later point". I honestly can't think of anything that phrase could mean that renders your claim accurate. If you can test the variable, it's valid. If you mean whether it points to an existent object, the test won't work for the reason I explained. – David Schwartz Feb 11 '20 at 17:19
  • 1
    @David Better after the edit? If not, feel free to edit yourself. I think you perfectly well know what I intend to convey. – Jesper Juhl Feb 11 '20 at 17:27
  • 1
    Honestly, I don't know. It's still wrong. That won't help if some other piece of code called `delete` on its copy of the pointer. It's not a generally recommended practice precisely because you can only use it in narrow cases where you happen to know for sure that it will work. There is, in general, no simple way to know if the value of a pointer is still valid other than carefully tracking it in a code-dependent way. Setting to `nullptr` without careful analysis is a bad habit that has the unfortunate consequence of leading to thinking it always works and making mistakes. – David Schwartz Feb 11 '20 at 17:29
  • 1
    @David that part of my answer is *only* about the narrow case where you use *this specific* variable later (it would commonly be a member variable) and you need to know if it holds a valid value or not. – Jesper Juhl Feb 11 '20 at 17:33