J3/99-217 Date: 23rd August 1999 To: J3 From: Malcolm Cohen Subject: Interpretation Request 71 on Character Array Constructors NUMBER: 000071 TITLE: Character array constructors KEYWORDS: Character, array constructor, zero-size DEFECT TYPE: STATUS: The f95 standard appears to leave the length of zero-sized character array constructors undefined. The penultimate paragraph of clause 4.5 says "The type and parameters of an array constructor are those of the expressions." If there are no elements, then the values of the type parameters might not be defined. The type and kind are always well defined at compile time. However, the length parameter for zero-sized character array constructors is not obviously well-defined, even at run-time. Zero-sized constructors are certainly allowed, as mentioned in the antipenultimate paragraph of clause 4.5: "An empty sequence forms a zero-sized rank-one array." Experimentation with different f90 compilers yields varying results. In the sample of question 2 below, one compiler gave a compile-time error message "array constructor has indeterminate character length". (Interestingly, it also gave that message when n=1, which should have been valid; some other variations gave compiler crashes). Another compiler gave the lengths as 2 and 5 and the n_calls as 1, obviously evaluating the presumed first element, even when the result had, in fact, no elements. QUESTION 1: Is the following a valid program and, if so, what len value does it print. program zero write (*,*) len( (/ ('abc', i = 1 , 0) /) ) end QUESTION 2: Is the following a valid program and, if so, what value does it print. program zero_size integer :: n = 0 call sub('abcdefghij', n) contains subroutine sub(string, n) character*(*) :: string integer :: n integer :: i write (*,*) len( (/ (string(:i+2), i = 1 , n) /) ) end subroutine end program zero_size QUESTION 3: Is the following a valid program and, if so, what values does it print for the lengths and for n_calls. program zero_size integer :: n = 0 integer :: n_calls = 0 call sub('abcdefghij', n) write (*,*) 'n_calls = ', n_calls contains subroutine sub(string, n) character*(*) :: string integer :: n integer :: i write (*,*) len( (/ (string(:f(i)), i = 1 , n), 'abcde' /) ) end subroutine sub integer function f(i) f = 2*i + 5 n_calls = n_calls + 1 end function f end program zero_size ANSWER: (1) Yes. The length is 3. (2) No. Edits supplied to make this illegal. SV: (i) A zero-sized char array cons shall have at least one that is an initialization expression or a that is not a ; (i-b) or a in whose all expressions are initialization expressions. (ii) A zero-sized char array cons shall have at least one whose character length does not depend on an ; (ii-a) and does not depend on the value of a non-intrinsic function reference (ii-b) a user function reference inside an may be executed even when the loop trip count is zero. (3) Yes. The length printed is 5, n_calls is still zero. EDIT: SUBMITTED BY: Richard Maine HISTORY: 98-101 m144 submitted 20 Nov 1997 99-207 m150 additional input 99-217 m150 proposed answer