Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FORTRAN 95, array must have constant shape error

Tags:

arrays

fortran

I'm an absolute beginner to Fortran95 so I apologise for the simplicity of this question. I want to be able define the dimensions of an array in terms of integers but I get the error

'The module or main program array 'arrayx' at (1) must have constant shape'

and 'Variable 'y' cannot appear in the expression at (1)' (same error for x). Is it simply not possible to define the dimensions in terms of variables or am I just missing something obvious.

program arrayForm
    implicit none
    integer, save :: x=40, y=2
    real, dimension(x,y) :: arrayX
end program arrayForm
like image 228
LlamaD Avatar asked Jan 12 '13 18:01

LlamaD


People also ask

Which error must have a constant shape in Fortran?

Module or main program array must have constant shape error in Fortran Ask Question Asked4 years, 5 months ago Active3 years, 9 months ago Viewed4k times 3 2 An integer variable declared in the module is used as a global variable to define the size of related arrays in the program.

Why must module or main program array must have constant shape?

Module or main program array must have constant shape error in Fortran - Stack Overflow An integer variable declared in the module is used as a global variable to define the size of related arrays in the program. The size of program varies, so the size of array is a variable but not a Stack Overflow About Products For Teams

What is an elemental procedure in Fortran 95?

Elemental procedures are specified with scalar dummy arguments that may be called with array actual arguments. In the case of a function, the shape of the result is the shape of the array arguments. Most intrinsic functions are elemental and Fortran 95 extends this feature to non-intrinsic...

How many dimensions can an array have in Fortran?

The lowest address corresponds to the first element and the highest address to the last element. Arrays can be one- dimensional (like vectors), two-dimensional (like matrices) and Fortran allows you to create up to 7-dimensional arrays. Arrays are declared with the dimension attribute.


1 Answers

Variables with the save attribute are not constants and the compiler complains with justification. In your snippet replace that attribute with the parameter attribute which makes them constant and, at compile-time, the array can be fixed in size and the compiler shouldn't complain.

As you have employed it the save attribute is redundant. When applied to variables which may go out of scope, such as variables inside the scope of a subroutine or function, it requires that the value of the variable is saved from one invocation of the sub-scope to the next. But since your variables are declared in your program scope they won't ever go out of scope during execution.

Note also that one of Fortran's gotchas for new (and old) programmers is that variables which are initialised in their declaration acquire the save attribute automatically. So, in a subroutine your line

integer, save :: x=40, y=2

could simply be written

integer :: x=40, y=2

Personally I tend to use the save attribute so that my intentions when I write a subroutine are clear to me later.

like image 143
High Performance Mark Avatar answered Oct 11 '22 08:10

High Performance Mark