Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Constructing an empty array

504 views
Skip to first unread message

Arjen Markus

unread,
Dec 22, 2010, 4:02:31 AM12/22/10
to
Hello,

I ran into a somewhat curious syntactical problem (or it is a problem
with the compiler I am using):

program test_print

call print( (/ /) )

contains
subroutine print( array )
real, dimension(:) :: array

write(*,*) array
end subroutine print
end program test_print

In this program I construct an empty array (in the actual program I
simply do not need it).

The Intel Fortran compiler, version 11.1, does not accept this (the
error message is not
related to array constructors though - it seems to expect a keyword
instead of /), which
makes me suspicious).

The gfortran compiler claims an empty array constructor is not
allowed.

My question: is this indeed the case? I can imagine there is a problem
in determining the
type of such a constructed array - is the only solution to define a
variable of type "real, dimension(0)" then?

Regards,

Arjen

Ian Harvey

unread,
Dec 22, 2010, 4:26:07 AM12/22/10
to
On 22/12/10 20:02, Arjen Markus wrote:
...

> My question: is this indeed the case? I can imagine there is a problem
> in determining the
> type of such a constructed array - is the only solution to define a
> variable of type "real, dimension(0)" then?


! Requires /assume:realloc_lhs with ifort 11.1
PROGRAM IFeelReallyEmpty
IMPLICIT NONE
REAL, ALLOCATABLE :: a(:)
!****
a = [ REAL:: ]
PRINT *, SIZE(a)
END PROGRAM IFeelReallyEmpty

Tobias Burnus

unread,
Dec 22, 2010, 5:09:03 AM12/22/10
to
On Dec 22, 10:02 am, Arjen Markus <arjen.markus...@gmail.com> wrote:
>      call print( (/ /) )

> The gfortran compiler claims an empty array constructor is not


> allowed.
>
> My question: is this indeed the case? I can imagine there is a problem
> in determining the
> type of such a constructed array - is the only solution to define a
> variable of type "real, dimension(0)" then?

gfortran is correct: An empty array constructor without a type-spec is
invalid as there the type of the array is not known. That causes
problems, e.g., when the compiler has to decide which of the generic
procedures should be called.

The solution is (cf. Ian's answer - though it is a bit hidden there)
to use a type-spec, e.g.

call print( (/ real :: /) )

which is a Fortran 2003 feature. In the standard it is encoded in the
following lines:

R468 array-constructor is (/ ac-spec /)
or lbracket ac-spec rbracket
R469 ac-spec is type-spec ::
or [type-spec ::] ac-value-list

Tobias

PS: I have filled a gfortran problem report (PR 47040) to print a more
helpful error message by mentioning "type-spec".

Arjen Markus

unread,
Dec 22, 2010, 5:21:19 AM12/22/10
to

Ah, so the message from the Intel Fortran compiler was actually quite
correct: it was looking for an indication of the type.

Thank you both for the clarification.

Regards,

Arjen

Ian Bush

unread,
Dec 22, 2010, 8:21:42 AM12/22/10
to
Tobias Burnus wrote:
> On Dec 22, 10:02 am, Arjen Markus <arjen.markus...@gmail.com> wrote:
>> call print( (/ /) )
>
>> The gfortran compiler claims an empty array constructor is not
>> allowed.
>>
>> My question: is this indeed the case? I can imagine there is a problem
>> in determining the
>> type of such a constructed array - is the only solution to define a
>> variable of type "real, dimension(0)" then?
>
> gfortran is correct: An empty array constructor without a type-spec is
> invalid as there the type of the array is not known. That causes
> problems, e.g., when the compiler has to decide which of the generic
> procedures should be called.
>
> The solution is (cf. Ian's answer - though it is a bit hidden there)
> to use a type-spec, e.g.
>
> call print( (/ real :: /) )
>
> which is a Fortran 2003 feature. In the standard it is encoded in the
> following lines:
>
> R468 array-constructor is (/ ac-spec /)
> or lbracket ac-spec rbracket
> R469 ac-spec is type-spec ::
> or [type-spec ::] ac-value-list
>


Or if you want to stick with f95

Wot now ? cat z.f90
Program z

Implicit None

Integer :: i

Write( *, * ) Size( (/ ( i, i = 1, 0 ) /) )

End Program z
Wot now ? nagfor z.f90
NAG Fortran Compiler Release 5.2(721)
[NAG Fortran Compiler normal termination]
Wot now ? ./a.out
0
Wot now ? gfortran -W -Wall -std=f95 -fbounds-check z.f90
Wot now ? ./a.out
0
Wot now ? ifort -stand f95 z.f90
Wot now ? ./a.out
0

Ian

Ian Bush

unread,
Dec 22, 2010, 8:41:26 AM12/22/10
to

(talking to myself as usual)

Or better -

Program z

Implicit None

Write( *, * ) Size( Pack( (/ 1 /), (/ 2 /) == 3 ) )

End Program z
Wot now ? nagfor z.f90
NAG Fortran Compiler Release 5.2(721)
[NAG Fortran Compiler normal termination]
Wot now ? ./a.out
0
Wot now ? gfortran -W -Wall -std=f95 -fbounds-check z.f90
Wot now ? ./a.out
0
Wot now ? ifort -stand f95 z.f90
Wot now ? ./a.out
0

1) No var required
2) It uses pack, therefore is clf approved

Combine with reshape as required.

All it needs now is transfer,

Ian

PATRICIASlater23

unread,
Dec 29, 2010, 6:03:08 AM12/29/10
to
People all over the world receive the <a href="http://bestfinance-blog.com/topics/credit-loans">credit loans</a> in various banks, just because it's simple.


0 new messages