Software Archive
Read-only legacy content
17061 Discussions

Initialization of data

lucaletizia
Beginner
544 Views
Hy Guys,

I am trying to inizialise the local varables when I declare them (and the same for the data in a UserDefined Type). But what I am finding is that when I call the subrioutine the first time averything works fine, if I call the same subroutine once more the values of the local variables are not the initialization value but the values which were given to the variables the previous time I have called the subroutine. Is it a Bug of the compiler (i have tryed on 6.6 and 6.5 and the behaivour is the same)?

Here is a brief example


Program Main 
Implicit None 
 
Type Report 
    Character (Len = 20) Report_Name 
    Integer Day 
    Character (Len = 3) Month 
    Integer :: Year = 1995 
End Type Report 
 
    Type (Report) Report1 
    Integer i, nInt 
    Real lReal 
 
    Do i = 1, 2 
        Call setData(Report1, nInt, lReal) 
        Call printData(Report1, nInt, lreal) 
    End Do 
 
Contains 
 
Subroutine setData(YourReport, YourInt, YourReal) 
    Integer YourInt 
    Real YourReal 
    Type (Report) :: MyReport = Report("Sales", 15, "Nov", 1996), YourReport 
    Integer :: MyLocalInt = 0 
    Real :: MyReal = 1.01 
 
    MyReport%Year = MyReport%Year + 1 
    MyLocalInt = MyLocalInt + 1 
    MyReal = MyReal * MyReal 
 
    YourReport = MyReport 
    YourInt = MyLocalInt 
    YourReal = MyReal 
 
End Subroutine setData 
 
Subroutine printData(YourReport, YourInt, YourReal) 
    Type (Report) YourReport 
    Integer YourInt 
    Real Yourreal 
     
    Print*, "Report Name: ", YourReport%Report_Name 
    Print*, "Day: ", YourReport%Day 
    Print*, "Month: ", YourReport%Month 
    Print*, "Year: ", YourReport%Year 
    Print*, "Integer: ", YourInt 
    Print*, "Real: ", YourReal 
    Print*, " " 
 
End Subroutine 
 
End Program
0 Kudos
2 Replies
Steven_L_Intel1
Employee
544 Views
No, this is not a compiler bug - it is the behavior specified by the Fortran standard. Variables that are DATA initialized or are initialized in their type specification statements (you did the latter) are given an implicit SAVE attribute and retain their values across calls. (F95 14.7.3)

There is an interesting subcase, though, that CVF used to get wrong (we fixed it in 6.5A, I think). If you have a derived type declaration in which you specify component initialization, local, non-ALLOCATABLE or POINTER variables of that type get the initialization reapplied on each entry to the routine (unless the variable also has SAVE explicitly specified.)

Steve
0 Kudos
Jugoslav_Dujic
Valued Contributor II
544 Views
No, that's as it should be. Data initialization gives the initial value at the time when the variable is created. Since variables are saved (static) by default, they are created and thus initialized only once (when they're first time encountered), as you notice.

So, you should use assignment statements instead. Have in mind that
 
INTEGER:: i = 0 

is different from
 
INTEGER:: i 
... 
i=0 

The latter assigns zero every time it's encountered; the former assigns zero only the first time the containing routine is executed. Note that in common speech both ways are usually referred as initialization, but when talking about uninitialized variable one means that neither kind of initialization was performed.

Jugoslav
0 Kudos
Reply