Re: Setting pointer to null!
"Doug Harrison [MVP]" wrote:
I'm afraid you'll have to explain to me what you're "differing"
with, because it seems to me you've restated what I said.
I disagree with the following statement:
"The best thing is to assign a non-NULL singular value." And
consequently: "In any case, if you "defensively" initialize all
pointers to NULL, you defeat [filling variables with predefined
value] debugging feature!".
Let me explain. I think this debugging fetaure emerged originally
in order to mitigate the uninitialized variables problem. When
developer forgets to initialize a variable and it accidentally
gets zero value at the run time, then this omission may exists
unnoticed for a long time. That's why the debugger cares to fill
uninitialized vars with deliberate garbage, so developer will
notice the omission on teh first attempt to use the variable.
This behavior of the debugger based on the premise that variables
should contain valid data for their lifetime. Valid data includes
zero value, which indicates that the variable is empty or
whatever. The same as the class invariant notion in the C++.
Indeed, the following initialization of the `p' pointer seems
redundant:
T* p = 0;
if (...)
{
... not using p
p = ...
}
else
{
... not using p
p = ...
}
However, we all know too well that such code quickly becomes
T* p = 0;
// do something else
if (...)
{
... not using p
p = ...
}
else
{
... not using p
p = ...
}
Where "do something else" part may involve an operation on the `p'
pointer. Once written even the excellent code becomes mediocre as
the time goes. Eventually mediocre code becomes bad code. This
kind of defensive programming will save the future devloper
unnecessary aggravation.
That's why it is often said that initializing a variable is a good
coding practice. It ensures that a variable contains valid data
throughout its lifetime.
Alex