Software Archive
Read-only legacy content
17060 Discussions

arrays of POINTERS

Intel_C_Intel
Employee
458 Views
I have been using the CVF syntax POINTER (p,MYSUB) to assign p the address of the subroutine MYSUB. I then modify the address of p using p=GetProcAddress(...). When I later call MYSUB, it is found at the new address. This seems to work very well. However, what I really need is for p to be an array of pointers, each of which points to a different subroutine and I am having trouble finding the proper syntax.

I declare the array of pointers INTEGER, POINTER :: p(:) and allocate space for it. I then use p(1)=LOC(MYSUB1) in order to associate MYSUB1 with p(1). Again, I use p(1)=GetProcAddress(...) to modify the address. However when I call MUSUB1 afterwards, it still has the old address.

I would highly appreciate any thoughts or comments that you might have and as always, many thanks in advance.
Cheers,
David
0 Kudos
2 Replies
Steven_L_Intel1
Employee
458 Views
Don't mix the Fortran 90 pointers with the integer pointer extension.

Get 6.5A, then do something like this:

INTEGER(INT_PTR_KIND()) P(n)

POINTER (PP, MYSUB)

Store your addresses in the elements of P, then when you want to use one:

PP = P(1)

CALL MYSUB (...)

Steve
0 Kudos
Jugoslav_Dujic
Valued Contributor II
458 Views
It's not 100% clear to me how you're trying to achieve it; it looks as if you mixed up a little integer ("cray") pointers and fortran pointers, which are quite different beasts (and, unfortunately, both are described with POINTER keyword, used differently in different contexts). Please note that POINTER in declaration INTEGER, POINTER:: p(:) has absolutely nothing related with use of pointers to functions. In order to simulate an "array of functions", it's enough to have an ordinary static or ALLOCATABLE array;
you fill it in with LOCs of functions and later dereference it, smt. like:

 
INTERFACE 
    SUBROUTINE GenericRoutine(arg1, arg2,...) 
    !arg declarations here 
END INTERFACE 
POINTER (lpfnGeneric, GenericFunction) 
 
INTEGER::  locFunction(20) 
 
locFunction(1)=GetProcAddress(hDll, szRealFunctionName1) 
locFunction(2)=GetProcAddress(hDll, szRealFunctionName2) 
... 
 
DO i=1,nWhatever 
      lpfnGeneric = locFunction(i) 
      CALL GenericFunction(...) 
END DO 


Note that you can literally use name GenericFunction for your subroutine: INTERFACE block above accompanied with a integer POINTER
only describes routine prototype, but routine GenericFunction exists only "in vain" until you assign address of an existing function (szRealFunctionNameX) to lpfnGeneric. When you call GenericFunction, you really call szRealFunctionName1, 2,...

HTH

Jugoslav
0 Kudos
Reply