0

To rephrase the title, say I have deleted pointer *ptr and I want to assign something new to it later on only when I know that it is open, should I assign it with NULL or nullptr? And if perhaps neither is good, what would be a better way to go?

From what I researched, NULL can be both an int and pointer type which could mess up when doing overloaded functions, while nullptr is only a pointer type. However, I still do not know when to use which. Should I always use nullptr for my unassigned pointers? If so, what is the purpose of NULL?

(btw, this is my first question on Stack Overflow so forgive me if I posted this somewhere where it doesn't belong)

EDIT: As for smart pointers, I can't really use them right now because I might get scolded by our professor.

  • 2
    If you are using `c++11` or above, prefer to use smart pointers, then avoid deleting by hand. For legacy code, just delete it without assign. – prehistoricpenguin Apr 28 '21 at 10:56
  • 3
    Dangling pointers and wild pointers are a plague in C++. If you feel better it cannot hurt to assign `nullptr` to the pointer after having deleted its contents (except if the pointer itself goes out of scope immediately after). On the other hand, to do it really better you drop the usage of `new` and `delete` completely and use smart pointers and containers instead. ;-) – Scheff's Cat Apr 28 '21 at 10:56
  • 3
    Use `nullptr` since it is the only right option since the advent of C++11. If you are managing your memory with `new` and `delete`, consider using smart pointers where applicable. – nvevg Apr 28 '21 at 10:57
  • `NULL` only exists for backwards compatibility with C, it's generally not used in C++ – IWonderWhatThisAPIDoes Apr 28 '21 at 10:59
  • I would like to use smart pointers, but it hasn't been taught to us yet. I don't want to deviate from our lessons or I might risk getting scolded by our professor for "copying answers from the internet". They ain't being practical, I know. Anyways, I'll be editing my question to include these info. Thanks! – Carl Eric Doromal Apr 28 '21 at 11:05
  • @AndreasWenzel That's actually what I was reading earlier. Yet, I still do not know the purpose of `NULL` now that `nullptr` exists. @IWonderWhatThisAPIDoes seems to have answered it though. So from what I understand now, `NULL` is pretty much just a remnant from C. – Carl Eric Doromal Apr 28 '21 at 11:14
  • 1
    I treat `delete p; p=nullptr;` as a code smell that indicates a developer hasn't thought enough about an object's lifecycle so I never assign to a pointer after`delete`ing it. It takes discipline, but it is feasible to ensure that a pointer immediately ceases to exist after being `delete`d, so cannot be accidentally misused e.g. a pointer that is a class member and `delete`d by a destructor ceases to exist when the destructor returns. One reason smart pointers (e.g. `std::unique_ptr`) are good practice is that destroying the manager object destroys the managed object in exactly that way. – Peter Apr 28 '21 at 11:23

2 Answers2

3

You should not use NULL in C++ at all. If you need a null pointer value, then use nullptr.

what is the purpose of NULL?

It is a remnant inherited from the C standard library. It has no purpose in C++ other than to make some C code valid C++ without changes - which used to be an important part of the design of the C++ language but has lost its importance over the years as the two languages have diverged.


And if perhaps neither is good, what would be a better way to go?

It is often better to make sure that the deleted pointer is a member whose super object is being destroyed. In such case there is no need to assign any value to the pointer since it is about to stop existing. For more details, see the RAII idiom.


You should avoid using bare owning pointers, and thus avoid using new and delete directly.

eerorika
  • 232,697
  • 12
  • 197
  • 326
-1

When you delete a pointer, you free the memory allocated to the pointer, created in a heap, i.e., dynamically. You can't delete anything not created by new or new[]. The name remains (unless the {braces} force it to expire) and you will be able to use it again and will not be able to use it otherwise.

Consider this code:

    
    int * pointer = new int; // a pointer created;
    *pointer = 1;//memory holds the value 1
    std::cout << "Value: " << *pointer << ", Location: " << pointer << std::endl;
    delete pointer;// pointer's memory is not used by the pointer

    pointer = new int;
    *pointer = 2;//memory holds the value 2
    std::cout << "Value: " << *pointer << ", Location: " << pointer << std::endl;
    delete pointer;// pointer's memory is not used by the pointer

Location can be the same or change (they would more likely change if in between the allocations you'd place any other allocation). This is heap, it doesn't really matter where it is stored. Try compiling it (inside int main(){}) and see yourself the locations on your machine.

When you assign something to a pointer, you tell him where to point to. This is different.

Basically, assigning a nullptr is what you need, so it shouldn't even be called a deletion. However, a pointer can well stay without any value to point unless you will try deleting it. And because deleting twice brings to undefined behavior, it should point to someplace, deletion of which will bring no harm.

In some circumstances, you create a program that deletes the memory pointed to by a pointer in any case and you won't check if the memory is allocated or deleted. (The most common example, perhaps, is move semantics.) Therefore, a nullpointer is always used there, as a part of a pattern.

Concerning NULL: macros become more and more obsolete in C++ as it moves towards freedom. It can bring to artifacts, so don't mess with it and make your life easier.

Mykola Tetiuk
  • 153
  • 1
  • 13
  • 1
    This doesn't really seem to answer the question of whether/why to "use NULL or nullptr" - instead only why one might want to nullify a pointer (in _either_ way) and a vague allusion to macros. But the question is a duplicate anyway, so doesn't need an answer. – underscore_d Apr 28 '21 at 11:17
  • This is the part of the question I addressed to: "And if perhaps neither is good, what would be a better way to go?" – Mykola Tetiuk Apr 28 '21 at 11:23
  • And here's one more part I answered with the answer you downvoted: "Should I always use nullptr for my unassigned pointers?" – Mykola Tetiuk Apr 28 '21 at 11:25
  • I beg your pardon. – Mykola Tetiuk Apr 28 '21 at 12:27