Sebastien Bardeau <bar...@no.domain> wrote:
> I am trying to fix the "uninitialized warnings" found in my code, and I
> can not get rid of the following one (see below).
>
> Those warnings seem to be very touchy, I could not shorten more my
> example. Note that I use the flag "-Wuninitialized" while gfortran
> returns "-Wmaybe-uninitialized" warnings.
>
> So, what does gfortran is warning me about? Is there a syntax that I
> could use to satisfy it? Usually adding a variable initialization or a
> missing "else" block is enough, but here I could not find.
[code elided]
I didn't try to verify the problem, but I don't see anything wrong with
the code as shown. Heck, the variable it warns about isn't used at all,
much less used when not "initialized".
Seems more like a quirk in gFortran's warning than a real problem.
I advise against adding code contortions that have no purpose other than
to work around warnings. That's particularly the case when the warnings
are bogus and come from only one particular compiler. If you go down
that path, your code can become full of cruft as you add workarounds for
multiple compilers. You might even find that the workarounds for one
compiler cause warnings for another.
A case of this kind of thing that I find particularly disconcerting is
when people take perfectly safe and standard-conforming code that
generates a warning, but they add a workaround fo rthe warning that can
actually make the code non-standard, but avoids the warning. I've seen
this in particular about "unused dummy argument" warnings. It can be
perfectly fine to have dummy arguments that are unused; it is natural
and common in some contexts (such as writing a particular case of a
subroutine that doesn't need all the data that the caling program
provides). Common "fixes" for this warning involve adding a spurious
reference to the variable, which has the potential of being an actual
error if the variable is undefined (which the subroutine can't know in
general). Workarounds to that error to make sure that the reference is
never actually executed can in turn cause different warnings with other
compilers. That's why I like compilers to have the capability to turn
off that particular warning, so that the numerous cases of it don't hide
warnings that I might actually care about.
I definitely encourage making sure to understand what warnings are
about, as you appear to be doing in your question here. Looks like it
might be warning either about the fact that some elements of the array
are undefined depending on the value of gweight, or that some elements
are never defined (all those in rows other than 1). But as long as you
never reference the potentially undefined elements, that's ok.
If the warning points to an actual problem, then certainly fix it, but I
wouldn't hack the code just to silence the warning.
As an aside, let me note that, independent of the question of whether
the warning is appropriate or not for this particulat case, I regard the
wording of the warning, and indeed even the switch name, as a compiler
bug that can cause confusion. The term "initialized" has a specific
technical meaning in Fortran, and it is the wrong term for this warning.
There is *NO* problem with using a variable that has not been
initialized. I cannot think of a single place where the standard
requires that a variable be initialized. Most variables in Fortran
programs are not initialized. There are many variables that it is not
even possible to initialize. What *IS* a problem is referencing a
variable that is undefined. Initialization is one way of making a
variable defined. But it is far from the only way or even the most
common way. Furthermore, even if a variable is initialized, it can later
become undefined.
People get confused enough anyway by what it means for a variable to be
undefined. I'm not sure whether whoever wrote that warning message was
confused themselves, or whether they thought that using different
terminology would help clarify things for users. If they thought it
would help clarify things, then I have to vehemently disagree. Maybe
that might work if you managed to come up with some other term that was
intuitive and wasn't otherwise used in the Fortran standard, I might buy
it. I can't offhand think of such a term, but I didn't try very hard,
and I grant the possibility that one might come up with such a thing.
But to use a term ("initialized") that does have meaning in Fortran, and
use it for something that is different from the meaning in the standard
strikes me as a way to add confusion rather than a way to avoid it.
That's even more so because the terms in question do have at least some
relationship - enough that someone might actually believe that the
message means what it says.
--
Richard Maine
email: last name at domain . net
domain: summer-triangle