0

i have quick question (i think quick). I have to check if pointer is NULL after delete data pointed by pointer. In my case i have data stored in fl->first and i want to clear this data. After clear by delete fl->first i have to check if fl->first pointer is NULL. I read a lot of posts that

delete fl->first;
fl->first = NULL;

is not good idea. And is there better way or in this case it is okey?

5
  • 1
    And did those posts specify why setting the pointer to null is a bad idea? Commented Mar 23, 2021 at 23:55
  • @UnholySheep It's not bad per se. But it can hide errors that the debug libraries can potentially find. Its also a waste of an instruction (if you decide that is important to you (looking at the embedded developers)). Commented Mar 23, 2021 at 23:58
  • @UnholySheep i found many opinions, but most of them is "Using pointer=NULL after delete can't hurt, but its better to dont do this" etc Commented Mar 23, 2021 at 23:59
  • this question is in large part to be sure that this NULL or maybe there is better way to do this (proper way). I dont want to learn bad habit Commented Mar 24, 2021 at 0:02
  • I think the point that most of the warnings are driving at is that the need to null a pointer after you delete the object it points to likely means you have muddled ownership semantics and/or aren't using appropriate smart pointers. Commented Mar 24, 2021 at 0:04

3 Answers 3

4

You should not use the NULL macro in C++. Using nullptr literal is preferable instead. This is because the macro is ambiguous in some cases which may lead to confusion.

Besides that, you should typically avoid owning bare pointers, and thus deleting anything directly is usually not a good idea.

Sign up to request clarification or add additional context in comments.

Comments

1

fl->first = nullptr; is C++ way of pointing to address of 0 in most cases (it does not mean that it is always 0). Why because in C you have void * in C++ you do not. You better read this.

That is as simple as it gets.

Longer answer is in void * you can allocate memory for 1000 chars, but you can read and write ints into them. To avoid this and for compilers to be more type safe C++ is going away from NULL and void *.

Comments

0

Yes this is a good idea, the reason being that it avoids crashes if you accidentally try to delete the same object again.

Example:

Foo* foo = new Foo(); // Create a new object
delete foo;           // Deletes the object
delete foo;           // Tries to delete an already deleted object and will therefor crash the program

Fix:

Foo* foo = new Foo(); // Create a new object
delete foo;           // Deletes the object
foo = 0;              // Sets the pointer to 0 (nullptr)
delete foo;           // Deletes nothing, the program will NOT crash

2 Comments

Better fix is to not write the second delete foo;. And to refactor the program in such way that it becomes impossible to do so accidentally.
@CEPB foo = 0 and foo = NULL (and foo = nullptr) are all identical if foo is a pointer. 0, NULL and nullptr are all null pointer constants. The thing that was being pointed out to you is that assigning the null pointer constant 0 to a pointer doesn't necessarily set all of its bits to 0. The actual bit pattern of a null pointer can vary by platform.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.