Is there a pointer value set for a freed pointer?

Some programmers like to set the pointer variable to null after releasing the pointer:

delete ptr; ptr = 0; 

If someone tries to free the point again, nothing will happen. In my opinion, this is wrong. Accessing the pointer after the pointee has been released is a mistake, and the errors should jump in your face as soon as possible.

Is there an alternative value that I could assign to a pointer variable that denotes freed pointers?

 delete ptr; ptr = SOME_MAGIC_VALUE; 

Ideally, I would like Visual Studio 2008 to tell me: "The program was interrupted because you tried to access the already released payer here!" in debug mode.


Well, I think I need to check myself. Is there something wrong with the following template?

 template <typename T> void sole_delete(T*& p) { if (p) { delete p; p = 0; } else { std::cerr << "pointee has already been released!\n"; abort(); } } 
+6
c ++ memory-management debugging pointers visual-studio-2008
source share
12 answers

Not. The test for "0" when you try to delete something, if you really want to warn about this or about an error.

Alternatively, during development, you can omit ptr = 0; and rely on valgrind to tell you where and when you are trying to do dual access. Just remember to return ptr = 0; for release.

Edit Yes, people I know, C ++ does not require testing around delete 0 ;

I do not suggest if (ptr != 0) delete ptr; . I suggest if (ptr == 0) { some user error that the OP asked for } delete ptr;

+7
source share

Assign NULL after releasing the pointer. And before using it, check for NULL ity .. If it is zero, report the error yourself .

+5
source share

Is there an alternative value that I could assign to a pointer variable that denotes freed pointers?

Ideally, I would like Visual Studio 2008 to tell me: "The program was interrupted because you tried to access the already released payer here!" in debug mode.

You will get this very likely just by doing delete ptr . Runtime will catch you if you delete this pointer twice.

In any case, I don’t think I wrote ptr = NULL more than a few times in the last decade. Why should I do this? Such a pointer, of course, is hidden inside the object, the destructor of which will delete the object to which it refers, and after this destructor has been called, the pointer also disappeared.

And if some circumstances require me to leave the hover pointer after deleting the pointer, I would not set it to NULL simply because I would like the code to crash as soon as possible if I delete twice. Setting the pointer to NULL only masks the error .

Of course, all this does not mean that you do not need a pointer that can be explicitly set to "nothing", and use NULL for this. But not to mask double deletion errors.

+4
source share

No, calling delete on a null pointer is completely normal from a C ++ point of view. Assigning some magic value will break the code a lot - now you have to distinguish between null pointers, real pointers and pointers to magic values, and I assume that it will be a huge mess.

If you really mind deleting the null pointer, you can have a separate boolean flag along with each pointer, meaning it was delete d. Perhaps you could write a wrapper class for this.

+2
source share

If you just want to check distributions and deletions, the easiest way is to write your own global operator new and operator delete and manually track all the pointers that are highlighted and freed.

Of course, you can also use an existing tool that does this for you, for example. Valgrind.

If you also want to log access to each pointer, it gets hairy. You, in fact, must either fix the executable file or execute it in a virtual machine, where every pointer access is redirected to your accounting program.

But then again, existing tools like Valgrind already do this for you. In the case of Valgrind, your executable is launched inside the virtual machine; other programs go the way of fixing your application by changing the byte code.

+2
source share

When you delete a pointer in debug mode, many compilers will draw bytes with some values ​​to indicate that the memory is "invalid" if you try to read it. Of course, genuine memory can have these bytes, so it allocates a little extra to indicate whether the pointer you are reading is valid or not, and draws bytes that you do not access directly.

It is wrong to cause deletion several times by the same pointer (variable) only by what it points to.

This may not be the best way to do this, but it is completely legal, of course ...

 T * array[N]; for( i = 0; i < N; ++ i ) { array[i] = new T; } T* ptr; for( i = 0; i < N; ++i ) { ptr = array[i]; delete ptr; } 

and, besides the fact that I'm not the best way to do something, I call delete on the variable "ptr" several times, but to different addresses and obviously not an error.

+1
source share

Answering the question [sic].

No, there is no set value for the highlighted pointers.

I think that any access to an invalid pointer (for example, NULL) should be noted - not only access to them after , which may not happen if there is no initialization without (NULL). The debugger must warn you when you try to access the null pointer - if it is not, you should not use it.

edit: end of the answer to the original question; double deletion interrupt

It really depends on the design if delete on NULL is an error awaiting appearance. In many cases this is not so. Perhaps you should use "safe delete" when necessary, or during debugging? Like this:

 template <typename T> void safe_delete(T*& ptr) { if (ptr == 0) throw std::runtime_error("Deleting NULL!"); delete ptr; ptr = 0; } 
+1
source share

I think sharptooth has already provided a valid answer , but I think he was unable to articulate it clearly:

If this is a mistake in your code to access the pointer variable after its object has been deleted through this pointer variable, then you should add some checks yourself. (Maybe through some flag.)

+1
source share

It makes no sense.

I will not enter, apparently, a rather heated conversation, I will simply indicate an obvious fact: pointers are transmitted using .

Using some code, it gives:

 T* p = /* something */; T* q = p; delete q; q = 0; 

Do you feel safe?

The problem is that you have no way to guarantee that your magic value has been passed to all pointers to the object.

It is like clogging a hole in a sieve and hoping that it stops pouring water.

+1
source share

In C ++ 0x you can

 delete ptr; ptr = nullptr; 
0
source share

Setting the pointer to a specific value only affects this copy of the pointer, so it almost does not protect. If the program must be reliably correct, there are classes of smart pointers that track copies of the pointer and make them invalid, otherwise I would recommend using a tool such as Valgrind (on Linux) or Rational Purify (on Windows), which allows you to check for access errors to the memory.

0
source share

This is not a delete nullpointer error; by definition, he does nothing.

This is usually a bad idea and a trade; to null pointer variables after delete , because the only effect that it can have is to hide the error that causes multiple deletion (with a pointer variable, null, the second deletion will have no effect, and not, for example, crash).

As a rule, zeroing pointers applies, in my opinion, to all other Microsoft: isms, such as Hungarian notation and the widespread use of macros.

This is something that may once have a good justification, but which today, since 2011, has only negative consequences and is used out of pure inertia: the dissemination of the idea of ​​the type that Knut once described for random generators - almost the least possible, popular and then included as the default generator in a wide variety of language implementations and libraries, with most people thinking that widespread use means that it should be at least reasonable.

However, having said that for a person who tends to be ultra-formal pedantic, he might be at least an emotionally satisfying idea for null pointers, for example. a std::vector , after delete . The reason is that the Holy Standard, ISO / IEC 14882, allows the std::vector destructor to do rather unholy things, such as copying pointer values. And in a formal pedantic view, even such copying of invalid pointer values ​​causes Undefined Behavior. Not that this is a practical problem. First of all, I don’t know absolutely no modern platform where copying will have some kind of bad effect, and secondly, so much code depends on standard containers that behave rationally, which they just need: otherwise no one will use such Implementation.

Greetings and hth.

0
source share

All Articles