passing pointer arrays of character strings

Hi,

I recently ran across the following problem and even though I seem
to have found the solution, I’m curious if the pgf90 compiler is following
the fortran standard, or if it’s a kind of bug. My previous compiler (sgi)
accepted the code and ran correctly, but then again that compiler is
known for accepting almost anything.

I’m trying to pass an array of character strings as a pointer
to a subroutine, and allocate it within the subroutine.

In the mainline, it’s declared as:

character(len=100), dimension(:), pointer :: data

If I declare it as a character(len=*) in the subroutine, then I get a
null pointer error when I try allocating it. When I change the
subroutine interface to pass in the string length and then
declare the array as:

integer, intent(in) :: nchar
character(len=nchar), dimension(:), pointer :: data

Then the program works. A short test program is attached.

Thanks for any insight,

Catherine

MODULE MOD1

CONTAINS

SUBROUTINE SUB_CHAR(DATA,NCHAR)

IMPLICIT NONE

INTEGER, INTENT(IN) :: NCHAR
CHARACTER(LEN=NCHAR), DIMENSION(:), POINTER :: DATA
INTEGER :: I

i = 1

allocate(data(i))

RETURN
END SUBROUTINE SUB_CHAR

SUBROUTINE SUB_INT(DATA)

IMPLICIT NONE

INTEGER, DIMENSION(:), POINTER :: DATA
INTEGER :: I

i = 1

allocate(data(i))

RETURN
END SUBROUTINE SUB_INT

END MODULE MOD1


PROGRAM PASS_POINTERS

USE MOD1, ONLY: SUB_CHAR, SUB_INT

CHARACTER(LEN=100), DIMENSION(:), POINTER :: DATA_CHAR
INTEGER, DIMENSION(:), POINTER :: DATA_INT

call sub_char(data_char,100)
call sub_int(data_int)

write(,) 'size_char = ',size(data_char,1)
write(,) 'size_int = ',size(data_int,1)

STOP
END PROGRAM PASS_POINTERS

Hi Catherine,

With your given example, I replace line 10 in module MOD1, subroutine SUB_CHAR with

CHARACTER(LEN=*), DIMENSION(:), POINTER :: DATA


And compile, it prints size of 1 for both size_char and size_int and I don’t see any null as you mentioned.

Which version of compiler do you use and which option? Can you post a program that gives you an error allocating and compile options you use?

Hongyon

Here is the compiler version I’m using and the compilation flags:

pgf90 6.1-6 64-bit target on x86-64 Linux

pgf90 -Mbounds -Mchkptr -Mextend -o pass.exe pass_pointers.f90

The error that I get upon running the attached program is:
0: Null pointer for data (pass_pointers.f90: 14)
which refers to the allocate statement in sub_char.

The test code is as follows:

MODULE MOD1

CONTAINS

SUBROUTINE SUB_CHAR(DATA)

IMPLICIT NONE

CHARACTER(LEN=*), DIMENSION(:), POINTER :: DATA
INTEGER :: I

i = 1

allocate(data(i))

RETURN
END SUBROUTINE SUB_CHAR

SUBROUTINE SUB_INT(DATA)

IMPLICIT NONE

INTEGER, DIMENSION(:), POINTER :: DATA
INTEGER :: I

i = 1

allocate(data(i))

RETURN
END SUBROUTINE SUB_INT

END MODULE MOD1


PROGRAM PASS_POINTERS

USE MOD1, ONLY: SUB_CHAR, SUB_INT

CHARACTER(LEN=100), DIMENSION(:), POINTER :: DATA_CHAR
INTEGER, DIMENSION(:), POINTER :: DATA_INT

call sub_char(data_char)
call sub_int(data_int)

write(,) 'size_char = ',size(data_char,1)
write(,) 'size_int = ',size(data_int,1)

STOP
END PROGRAM PASS_POINTERS

Catherine

Hi Catherine,

There is a bug in 6.1-6 with -Mchkptr. Please use our latest release 7.1-3.

To avoid this problem in 6.1-6, remove -Mchkptr from the options.

Hongyon