sorry about that - the comment was a little bit out of context of your question.
what I meant was, suppose you had:
1 2 3
|
int* ptr = new int;
*ptr = 5;
delete ptr;
|
some people believe that Line 3 will free memory and set ptr to NULL - in fact, it only frees memory.
as a further illustration, you could copy Line 2 to Line 4 if you like. Many beginners think Line 4 would cause a crash, but in fact, you will get
indeterminate behavior - it may crash at Line 4 or it may crash somewhere far away. This is much worse than a certain crash at Line 4 - sometimes, a segfault is your friend, if it crashes right at the point of your bug. If you do thread programming one day, you will find that the same difficulties apply - one of the reasons thread programming is difficult is the ease of creating race conditions that cause indeterminate behavior. It crashes, then it doesn't crash, then it crashes again five times, but then doesn't!
this is relevant for debugging because sometimes, when ptr to nothing is explicitly set to NULL, it is easier to locate the problem - it often makes your crashes determinate. For example, if you did this:
1 2 3 4 5
|
int* ptr = new int;
*ptr = 5;
delete ptr;
ptr = NULL;
*ptr = 5; // will definitely crash here
|
this is a Mickey-Mouse example. In real code, the pieces will be farther away from each other and not so obvious. However, this example illustrates that programming in C++ requires discipline and diligence, if you do not want to spend a lot of time debugging.