Re: Setting pointer to null!
"Doug Harrison [MVP]" wrote:
In any case, if you always initialize pointers to NULL,
including ones for which NULL is not a valid value at that point
in time, you defeat the debugging feature, because you prevent
the compiler from initializing them with the special pattern.
I think I understand now what you're trying to say. Probably it's
the misunderstanding on my side, but I have never considered the
NULL value as the invalid one for pointers. For me a valid pointer
can contain either a valid address of a pointee, or NULL (which
indicates that the pointer isn't assigned any value yet).
The special value that debugger puts in uninitialized variables is
a good thing. I can't argue with that, of course. However, I treat
this special value as invalid for a pointer and ultimately a bug
in a program. That's why when you mentioned logic errors due to
NULL value I didn't understand you properly. I am accustomed to
include possible NULL value of a pointer into program logic.
But 0 is not a valid value for every variable at every point in
time. Just because some value is in the domain of a type does
not imply it's valid for every variable of that type. It may
impart a "predictable" behavior, but it doesn't mean it's valid,
and invalid data can cause incorrect behavior, as I've already
described a couple of times.
Yes, after I read this I understood why you called 0 an invalid
value. I dont have any strict definition of an invalid value to
quote here. So, I invented my own definition which includes 0
(sometimes as a special case to indicate that the variable is
"empty") along all other valid values. Exactly how it happens to
be in Java or C# where newly declared variable contains 0 by
default.
I can't agree that's even remotely common, simply because you
don't just start using a variable without knowing anything about
it. Or if you do, you quickly learn what a mistake it is, and
you stop.
I agree with you in general, but the "quickly learn" part is not
quick enough sometimes. This is the only reason I ensure to
initialize all variables I declare. NULL value lets me to learn
the problem quicker than special debugger value.
In C++, it especially does not happen, because there's no reason
to extend the lifetime of p in that way.
True. I strive to declare variables as close as possible to their
use for that reason. However, there is a lot of C legacy code
which is still in production. Also, there are many developers who
still have this C habit to declare variables at the top of a
function.
Look at the examples here:
http://java.sun.com/docs/books/jls/second_edition/html/defAssign.doc.html
I can't believe you would insist upon initializing every "k" in
these examples.
You right, I wouldn't insist upon initializing every "k", but I
would try to rewrite the code to avoid this problem. For example
in this case:
int k;
if (v > 0 && (k = System.in.read()) >= 0)
System.out.println(k);
I would prefer
if (v > 0) {
int k = System.in.read();
if (k >= 0)
System.out.println(k);
}
which is more clear and doesn't pollute the enclosing scope with
`k' name as an added bonus.
However, in this example:
int k;
while (true) {
k = n;
if (k >= 5) break;
n = 6;
}
System.out.println(k);
I will initialize `k' before the "while" statement even if such
initialization looks redundant.
Alex