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

Passing Allocateable arrays in subroutine arguments

steve_konarski
Beginner
744 Views
Is it possible to pass allocatable arrays as subrountine arguments

I have a subroutine



SUBROUTINE ALLOCATE_ARRAY(PC_ID, PC_ID_SIZE, ISV)

IMPLICIT NONE

CHARACTER*100, ALLOCATABLE::PC_ID(:)

INTEGER PC_ID_SIZE,ISV

INTEGER IERN

CHARACTER CBUF

IF (ALLOCATED(PC_ID)) DEALLOCATE (PC_ID, STAT=ISV)

ALLOCATE (PC_ID(PC_ID_SIZE), STAT=ISV) ! ALLOCATE PC_ID to a size of 500

IF (ISV .NE. 0) THEN

C FATAL ERROR - RUN TERMINATED

IERN = 2

CALL ERROR (IERN, CBUF)

C MEMORY ALLOCATION ERROR

IERN = 73

CALL ERROR (IERN, CBUF)

ENDIF

RETURN

END

called from a main program ( this has been reduced for the forum)


USE COMMON_PARENTCHILD

IMPLICIT NONE

INTEGER ISV

PC_ID_SIZE = 500

CALL ALLOCATE_ARRAY(PC_ID,PC_ID_SIZE,ISV)

STOP

END

with the module

MODULE COMMON_PARENTCHILD

CHARACTER*100, ALLOCATABLE::PC_ID(:)

INTEGER PC_ID_SIZE

END MODULE COMMON_PARENTCHILD

and an error


Error 1 error LNK2001: unresolved external symbol _ALLOCATE_ARRAY@16 progdll_lib.lib progdll

is shown


steve

0 Kudos
5 Replies
Arjen_Markus
Honored Contributor II
744 Views
For such subroutines the compiler must see an explicit interface. The easiest
way is to put the subroutine in a module.

The error you quote seems to be caused bynot exporting the allocate_array subroutine.

Regards,

Arjen
0 Kudos
mecej4
Honored Contributor III
744 Views
There are three points that you do not appear to have given due consideration.

1. An allocatable array may be passed as an actual argument to

(a) a subroutine that expects an ordinary array argument in the corresponding position.

(b) a subroutine that expects an allocatable array argument in the corresponding position.

In case (a), the actual argument may be either a statically allocated array or a dynamically allocated array, and no explicit interface to the subroutine is needed in the caller.

In case (b), the actual argument must be ALLOCATABLE, and an explicit interface to the subroutine is needed in the caller. Without an explicit interface provided, the compiler will produce an incorrect subroutine linkage.

2. If the called subroutine is in a DLL and the caller is in a separate application, extra care is needed to provide the explicit interface when one is needed. You have not made it clear as to which subroutines are in the DLL, how the DLL is built and called.

3. You have used the STDCALL linkage in your DLL or EXE. There needs to be a good reason for its being used, and consistency in its being used.
0 Kudos
Arjen_Markus
Honored Contributor II
744 Views
Hm, you are absolutely right. I will add: STDCALL has been deprecated for a long time, and
in Intel Fortran it is not the default. So someone has taken a deliberate action to turn it on (the
visible effect is the suffix "@16" to the subroutine name).

Regards,

Arjen
0 Kudos
Steven_L_Intel1
Employee
744 Views
I need to correct some misimpressions here.

There are absolutely no restrictions on passing an allocatable array as an actual argument that don't apply to any other kind of array. The language does provide requirements for an explicit interface depending on how the dummy arguments of the called routine are declared, but this is independent of the form of the actual argument.

In particular, there is no requirement that the dummy corresponding to an allocatable array also be ALLOCATABLE - in most cases you don't need or want that. The only case where you would want ALLOCATABLE on the dummy is if you plan to change the allocation status in the called routine.

Now as to the STDCALL - this program is probably being compiled with /iface:cvf, especially if it was migrated from CVF. In most cases, you can turn off that option.
0 Kudos
steve_konarski
Beginner
744 Views
I corrected the problem with the answer given by ARJEN - (thanks) in Reply 1, it is a self contained program - I will look into the other replies - many thanks steve
0 Kudos
Reply