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

Initialisation of array in data structure

Stephen_Sutcliffe1
470 Views

I have created a data structure that contains an array:

type EXAMPLE
  real*8 :: T(3,3)
end type
real*8 :: T8(3,3) 

type(EXAMPLE) :: array

To initialise a normal variable array like T8 I can use the following statement:

T8 = (/1.0d0,0.0d0,0.0d0,0.0d0,1.0d0,0.0d0,0.0d0,0.0d0,1.0d0/)

however when I try to do this with the data structure thus:

array%T =  (/1.0d0,0.0d0,0.0d0,0.0d0,1.0d0,0.0d0,0.0d0,0.0d0,1.0d0/)

I get the following compiler error:

Compiling with Intel(R) Visual Fortran Compiler XE 13.1.3.198 [Intel(R) 64]...

C:\Win\FEM2000_v25\Modules\FluxLib.f90(53): error #6366: The shapes of the array expressions do not conform.
compilation aborted for C:\Win\Modules\FluxLib.f90 (code 1)

However when I use this:

array%T(1,1:3) = (/1.0d0,0.0d0,0.0d0/)
array%T(2,1:3) = (/0.0d0,1.0d0,0.0d0/)
array%T(3,1:3) = (/0.0d0,0.0d0,1.0d0/)

there is no problem.

Is this behavior expected?

Thanks

Steve

0 Kudos
5 Replies
A__Valle
Beginner
470 Views

Steve,

I would expect that you need the following command for both data structure initializations:

T8 = RESHAPE((/1.0d0,0.0d0,0.0d0,0.0d0,1.0d0,0.0d0,0.0d0,0.0d0,1.0d0/),(/3,3/))

Dirk

0 Kudos
Stephen_Sutcliffe1
470 Views

Hi Dirk,

I agree that both data initialisations should be consistent but they aren't. The reshape function does not seem to be required for arrays of statndard data types. I'd be interested to known if this is intentional or a compiler glitch.

0 Kudos
Steven_L_Intel1
Employee
470 Views

Stephen, are you sure?

C:\Projects>type t.f90
real a(3,3)
a = (/1,2,3,4,5,6,7,8,9/)
end
C:\Projects>ifort -c t.f90
Intel(R) Visual Fortran Compiler XE for applications running on IA-32, Version 14.0.1.139 Build 20131008
Copyright (C) 1985-2013 Intel Corporation.  All rights reserved.

t.f90(2): error #6366: The shapes of the array expressions do not conform.  

a = (/1,2,3,4,5,6,7,8,9/)
^
compilation aborted for t.f90 (code 1)

0 Kudos
Stephen_Sutcliffe1
470 Views

Ah my mistake I see now,

The situation where you can do it is this when variable is declared.

real:: a(3,3) = (/1,2,3,4,5,6,7,8,9/)

Why is this not allowed in executable part as omitting the reshape function could default to shape of the original declaration?

I suppose the shape of the arry could be changed anywhere during the program execution so I think I understand why you need to specify the shape when assigning a list of values in a non-declaration situation.

0 Kudos
Steven_L_Intel1
Employee
470 Views

It's curious that the standard requires that for an intrinsic assignment statement:

"the variable and expr shall be conformable" (7.2.1.2, paragraph 1, (3))

The definition of "conformable" is "(of two data entities) having the same shape, or one being an array and the other being scalar"

So in the context of an intrinsic assignment statement, the shapes are required to be the same (there's an exception for an allocatable array for the variable, though the rank can't change.)

But for initialization, traditionally DATA let you initialize multidimensional arrays with a series of values and this carried over to initialization on a type declaration statement. There, the standard says:

"if necessary, the value is converted according to the rules of intrinsic assignment (7.2.1.3) to a value that agrees in type, type parameters, and shape with the variable."

Note that the section referenced isn't the same as for the intrinsic assignment statement - 7.2.1.3 says:

"If the variable is an array, the assignment is performed element-by-element on corresponding array elements of the variable and expr."

So the rules really are different and the compiler correctly enforces that.

0 Kudos
Reply