Re: Component Initialization of Module Variables



Richard Maine wrote:

<michaelmetcalf@xxxxxxxxxxxxxx> wrote:

On Mar 4, 3:50 pm, Janus Weil <jaydu...@xxxxxxxxxxxxxx> wrote:
[code elided]
This is rejected by g95 with

Error: Module variable 'tve' at (1) with a component
initialization must have the SAVE attribute

while it is accepted by the other compilers I tried (gfortran
& ifort). Now my question: Is g95 right to reject this, or is
it a bug? My guess is that since VE_real is ALLOCATABLE,
there should be no default initialization, and the error
message is bogus. Right?

You say you tested this with ifort, but my version also,
correctly, gives a warning message. Objects like these, when
declared in a module, are required to have the save attribute
("Fortran 95/2003 Explained", Section 7.5.4).

I didn't go back and check the standard to make sure all was
correct, but probably so. It sounds familiar and I'll assume
Michael has it right.

I'm just posting to ask why the OP would think that default
initialization would not apply to allocatables in any case. That
would be an oddity in the standard. While it does have several
oddities, I don't know of that one.

<<<--- CUT --->>>

From section 7.5.4 of MR&C: "...default initialization does not
imply that the objects have the 'save' attribute. However, an
object of such a type that is declared in a module is required to
have the 'save' attribute unless it is a pointer or an
allocatable array. This is because of the difficulty that some
implementations would have with determining when a non-saved
object would need to be re-initialized."

The OP may have read this excerpt before posting. To a normal user
who does not worry about how the compiler would go about
implementing the attributes, the error message would appear to be
a compiler bug. And, the fact that one compiler signals 'error'
and another 'warning' suggests that it may be not an easy feat to
extract the rules from a standards documents, which itself is not
available at reasonable cost.

-- mecej4
.



Relevant Pages

  • Re: Uninitialized variables
    ... > unintialized variables to zero on the old CVF forum, ... > eems reals and integers have a default initialization of the largest ... The Fortran compiler no longer "owns" the linker/OS ... all of the time and the standard doesn't ...
    (comp.lang.fortran)
  • 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: Component Initialization of Module Variables
    ... who does not worry about how the compiler would go about ... from the standard and from textbooks, leaving just the error message as ... has "component initialization", which isn't a term used in the standard, ...
    (comp.lang.fortran)
  • Re: initializing static class variables
    ... >> I believe it was introduced in 2003 standard but I can't be sure since I ... >> default constructor is called. ... the definition of compiler generated default constructor. ... constructed with default initialization. ...
    (comp.lang.cpp)
  • 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)