Re: Use of unassigned local variable?
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Mon, 29 Jan 2007 18:07:50 -0800
On Mon, 29 Jan 2007 03:59:40 -0800, Jon Skeet [C# MVP] <skeet@xxxxxxxxx> wrote:
Does the C++ compiler complain about unassigned local variables at
all? (I've no idea.)
Yes, you can turn that warning on (and of course, you can set the compiler to treat the warning as an error if you like).
Personally, I think it's well and good to keep things simple. However, the problem here is that the unassigned variable is always an error but the compiler incorrectly detects provably-assigned situations as unassigned. I understand keeping the language definition simple, but in this case the very act of changing the code to get rid of the error can lead to the very bug that the error would otherwise be protecting against. That is, by requiring the programmer to provide a default value, if the code is changed later so that the variable is not *properly* initialized (there may in fact be no appropriate "default" value, even though the compiler insists on it), then what is essentially an "uninitialized variable" bug can easily creep in, even as the compiler has an error that's supposed to detect and prevent such a bug.
We've had this discussion before, and I admit you may not find my point of view compelling. But personally, it's my opinion that if a compiler cannot definitively determine a situation, it has no business emitting errors regarding that situation. It's fine for the compiler author and/or language designer to say that the compiler shouldn't be required to analyze loop conditions when deciding whether a variable is initialized or not, but if that's the choice that's made, the compiler shouldn't be making the uninitialized variable condition an error.
Personally, I don't think it would be all that hard for the compiler to deal with invariant loop boundaries and properly determine whether the loop (or other block of code, for that matter) is always executed or not. I also don't think it would be all that confusing to the programmer for some loops to be flagged as "always entered" or "never entered" while other loops are treated as "don't know" (and thus, for the uninitialized variable situation treated as "never entered", that being the worst-case scenario).
Pete
.
- References:
- Re: Use of unassigned local variable?
- From: Peter Bradley
- Re: Use of unassigned local variable?
- From: Jon Skeet [C# MVP]
- Re: Use of unassigned local variable?
- From: Jon Skeet [C# MVP]
- Re: Use of unassigned local variable?
- Prev by Date: Re: threads
- Next by Date: Re: Use of unassigned local variable?
- Previous by thread: Re: Use of unassigned local variable?
- Next by thread: Re: Use of unassigned local variable?
- Index(es):
Relevant Pages
|