Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

bad code, but no warning

prop_design
New Contributor I
398 Views

hi,

 

i ran into something today that surprised me. i left off the array designation on three variables. the compiler didn't notify me of the error. when i run the code those variables have numbers associated with them. however, they are wrong. once i found the error, myself, i added the array designation and the numbers are now correct. i'm wondering why the compiler didn't notify me of this. here is a simple example:

 

correct:

  variable(j) = 1.0d0

 

incorrect:

  variable = 1.0d0

 

the variable was defined as an array, using REAL*8

 

the only thing i can think is maybe there is a flag you have to set for the compiler to notify you of such things.

0 Kudos
1 Solution
mecej4
Honored Contributor III
395 Views

Array assignments, array expressions, etc., are an integral part of Fortran since Fortran 90. The statement that you showed causes all the elements of array VARIABLE to be set equal to 1.

You should not expect any compiler to give you warnings with regard to code that is perfectly normal modern Fortran. If you can find a Fortran 77 compiler, you can run that on your code and it will give error messages for array assignments.

Running the f2c translator on the following file

      subroutine process(V,n)
      integer n, i
      real V(n)
      V = 1.5
      return
      end

  gives

S:\LANG>c:\LANG\F2C\f2c.exe array.f
array.f:
   process:
Error on line 5 of array.f: wrong number of subscripts on v

View solution in original post

5 Replies
mecej4
Honored Contributor III
396 Views

Array assignments, array expressions, etc., are an integral part of Fortran since Fortran 90. The statement that you showed causes all the elements of array VARIABLE to be set equal to 1.

You should not expect any compiler to give you warnings with regard to code that is perfectly normal modern Fortran. If you can find a Fortran 77 compiler, you can run that on your code and it will give error messages for array assignments.

Running the f2c translator on the following file

      subroutine process(V,n)
      integer n, i
      real V(n)
      V = 1.5
      return
      end

  gives

S:\LANG>c:\LANG\F2C\f2c.exe array.f
array.f:
   process:
Error on line 5 of array.f: wrong number of subscripts on v
prop_design
New Contributor I
382 Views

hi @mecej4 

 

oh. i forgot to say, yes i'm using fortran 77. i don't know anything about modern fortran. my example was too simple for what i actually coded. let me try again:

 

real*8 variable1(10), variable2(10)

 

what i wrote:

       do 10 j = 1, 10

       variable1 = variable2(j)

 10 continue

 

what i should have wrote:

       do 10 j = 1, 10

       variable1(j) = variable2(j)

 10 continue

 

      do 20 j = 1, 10

      write(*,*) variable1(j), variable2(j)

 20 continue

 

so both variables were defined as arrays, using real*8. i accidentally left off the (j) in the source code and didn't notice for a few weeks. the code compiled with no warning but variable1 was filled with random numbers (i think). not really sure what or how it had anything at all. given i screwed the code up. it would have been really helpful it it didn't compile and warned me.

 

0 Kudos
andrew_4619
Honored Contributor II
350 Views

That may be stupid code but it is still valid Fortran. IFORT standards check options are Fortran 90 onwards there isn't a 77 option.

 

prop_design
New Contributor I
346 Views

thanks @andrew_4619 

 

i guess me not knowing new fortran is a big disadvantage here. how can something like var be considered the same as var(j)? in fortran 77 if you want something to be an array you have to specify it. i did that. if you want it to be a constant then you specify that. is new fortran something completely different?

 

in any event, i guess that's just the way the compiler is.

0 Kudos
andrew_4619
Honored Contributor II
344 Views

If you declare 2 arrays A and B the same size then A=B is valid and copys B into A

The other case is C real scaler  then A=C is valid C is assigned to all elements of A. Saves a lot of boring loops being both typed and executed.

 

integer :: I, J, K
Real :: A(10,100,5)
do K = 1 , 5
   do J = 1 , 100
      do I = 1 , 10
         A(I,J,K) = 0.0
      enddo
    enddo
enddo

!Or
Real :: A(10,100,5)
A = 0.0


 

 

Reply