Re: try...catch and local variables

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Mythran" <kip_potter@xxxxxxxxxxx> wrote in message
news:uZ5b58YDHHA.1196@xxxxxxxxxxxxxxxxxxxxxxx
By just looking at the code and error, I see you are in fact not assigning
oProcessFileReader before using it. Even though you are checking for
null, the compiler isn't smart enough to notice and it results in the
error you are getting.

The compiler does notice that he's checking for null. The problem is that
he's not allowed to check for null until he's set the variable to null. He
really does have an uninitialized variable bug in his code.

[...]
Note: This is simply a compiler error and I'm not sure if you can turn
off this functionality. It exists to prevent you from doing something
more like:

StreamReader reader;
reader.ReadLine();

Which is obviously bad, so the compiler warns/throws a tantrum when you
try to do something that even remotely resembles this...

In this particular case, there *is* a code path that can result in the local
variable being accessed before being initialized, without initialization in
the declaration. Initializing the local variable is indeed the correct
solution to the error, and is also correct relative to the rest of the code
that was posted.

However, unfortunately the compiler sometimes also incorrectly complains
that the variable is uninitialized, even when all code paths *do* initialize
the variable before it's accessed.

IMHO, this is a serious compiler bug, because the only way to get rid of the
error is to put in a "dummy" initialization (such as setting the variable to
null), which consequentially will hide any true "uninitialized variable"
errors in the code.

In other words, with the compiler in the state in which it is now, there are
actually situations in which the compiler forces the programmer to write
code that is *more* likely to contain the bug that the error is trying to
help avoid. I'd find the irony very entertaining, if it weren't for the
fact that there's a serious code quality issue related to it. :)

Pete


.



Relevant Pages

  • Re: OT: Requesting C advice
    ... some behind the scenes action of the compiler. ... In fact the memory could ... Proper initialization means that floats and doubles must be initialized to 0.0 and pointers must be initialized to the null pointer value, even if those bit patterns differ from all-bits-zero. ...
    (Fedora)
  • Re: I think C# is forcing us to write more (redundant) code
    ... static void F(out string s) ... >> compiler enforced). ... locals here and locals are stack allocated, ... I don't have a problem with this explicit initialization in the current ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: memory management in Ada: tedious without GC?
    ... I don't like the permission to reorder operands, ... May the compiler group A and C and initialize both them by zeroing memory? ... If the initialization order were fixed, that would be illegal to do. ... subprograms, not as a pragma Pure, but as a contract. ...
    (comp.lang.ada)
  • Re: pointer and storage
    ... But, as always, a compiler is free to issue any diagnostics it ... If the initialization is executed, it invokes undefined behavior. ... In old versions of the C language, before "const" was introduced, this ...
    (comp.lang.c)
  • Re: inhibit compiler warning C4624 for a class hierarchy
    ... The undefined behavior means that the behavior is decided by the compiler. ... could appear due to member destructors not being called, ... I guess I can make the two-step initialization ... Compiler behavior would be well defined by the standard. ...
    (microsoft.public.vc.language)