-1

In C or C++ when we delete some pointer, it only frees the memory but does not set the pointer to 0. Since we can not check the validity of a pointer, it would have been easier for the programmer to check the nullness of the pointer if the pointer is set to 0 after freeing the memory.

I was just wondering why 'delete' is implemented only to free the memory.

learning
  • 94
  • 6

6 Answers6

4

“Since we can not check the validity of a pointer, it would have been easier for the programmer to check the nullness of the pointer if the pointer is set to 0 after freeing the memory.”

Checking for nullvalue is likely to hide bugs while increasing the code size and complexity, plus giving a false sense of security. There is no guarantee that the pointer value (before nulling) hasn't been copied elsewhere. Also, this prevents delete by value, which is a major annoyance with libraries (such as OpenCV) that, misguided, offer nulling delete operations.

Instead of such counter-productive practice, use techniques that ensure proper cleanup and prevent invalid pointers, such as using appropriate smart pointers.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Thanks Cheers and hth. - Alf, "There is no guarantee that the pointer value (before nulling) hasn't been copied elsewhere" -- This answered my question. – learning Aug 02 '12 at 13:40
1

Because, in C++, it (almost) all about performance. You don't want the compiler to add code, you don't need. You don't want your program to do things, that you haven't added them in your code.

If you're sure, that on one will check/reuse this pointer, there's no need for annulling. One instruction less.

Also, if deleting a pointer, sets it to NULL, what will happen with the other pointers, that point to the already deleted memory? They will not be annulled. This could lead to bad things.

Just assign NULL, if you need it.

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • I regard this as being a good answer. That in C++ the compiler shouldn't do things for you that have a cost, if you might not need the action AND you can implement it yourself. My deletes are almost always in the destructor of a class, so the pointer isn't going to exist shortly afterwards anyway so why would I want to pay the few nanoseconds cost of a useless assignment. It's easy enough to write a "safeDelete" template function if you want it to do that and call it instead of delete – jcoder Aug 02 '12 at 14:44
  • @JohnB - my point, exactly :) – Kiril Kirov Aug 02 '12 at 14:46
  • YEs, just agreeing. Unnecessarily :) – jcoder Aug 02 '12 at 14:51
1

There's no need: Since you only delete in the destructor of your SBRM wrapper class, there's nothing else that could possibly access the pointer afterwards:

template <typename T> struct MyPtr
{
     template <typename ...Args> MyPtr(Args &&... args)
     : p(new T(std::forward<Args>(args)...)
     {   }

     ~MyPtr()
     {
         delete p;   // done! who cares what `p` is now.
     }

     MyPtr(MyPtr const &) = delete;
     MyPtr & operator=(MyPtr const &) = delete;

     T * operator->() { return p; }

private:
     T * p;
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Could you elaborate on SBRM? A google returns nothing obvious. – cmh Aug 02 '12 at 13:36
  • Just call destructor twice :) –  Aug 02 '12 at 13:37
  • 2
    @cmh it's a fancy alternative name for RAII. Stands for Scope-Based Resource Management. – R. Martinho Fernandes Aug 02 '12 at 13:37
  • Thanks, would be good if the full def. was in the question for those that haven't seen it before. – cmh Aug 02 '12 at 13:38
  • @cmh: It's "scope-based resource management", a core C++ idiom. It is sometimes colloquially referred to as "RAII" ("resource acquisition is initialization") in the literature. It boils down to making *all* resources local by use of automatic, scoped variables. C++ provides "magic `goto`s" by means of destructors, and those allow you to write control flow with multiple exits with ease. "Never have to touch anything more than once", if you will. – Kerrek SB Aug 02 '12 at 14:15
0

It would not make anything safer, even if delete assigned NULL to the pointer. Since multiple pointers can point to the same memory address and assigning NULL to one of these will not make others NULL as well.

mostar
  • 4,723
  • 2
  • 28
  • 45
0

You can delete a const pointer variable. Setting it to NULL is not possible, Only setting mutable pointer variables to NULL after delete seems somewhat inconsistent.

smerlin
  • 6,446
  • 3
  • 35
  • 58
0

For one thing, you might want to do pointer arithmetic after delete for efficiency reasons:

const int SIZE = 5;
MyObject* foo[SIZE];
// ... initialize foo with new MyObjects...
for (MyObject* i = &foo[0], end = &foo[0] + SIZE; i < end; ++i)
{
    delete i;
}
Nathan Monteleone
  • 5,430
  • 29
  • 43
  • I don't see this as a compelling reason, at least not with this example: #1 there are far better alternatives (an array of `std::unique_ptr`, `boost::scoped_ptr`, or even `std::auto_ptr` would be better) and #2 it is highly unlikely the pointer arithmetic will generate more efficient code than a loop through indices instead of pointers. – R. Martinho Fernandes Aug 02 '12 at 13:40
  • @r_martinho_fernandes Agreed, there are much better ways to do it now. C++ didn't always have smart pointers readily available though. As to point #2, yes it's unlikely, but C++ at least in the original implementation always tried to leave that sort of optimization available. – Nathan Monteleone Aug 02 '12 at 13:52