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

Do 2 pointers to 2 functions require 2 interfaces?

dmw
Beginner
739 Views

Do 2 pointers to 2 functions require 2 interfaces?

Do 3 pointers to3 functions require3 interfaces?

and so on


The question relates to CVF 6.6 and arises from the use of LoadLibrary and GetProcAddress to call functions from a DLL.

However, I think my question concerns the language.

If I have 2 functions with the same 'signature' (return type, argument types) and I want 2 function pointers pointing to each one, ...

... Do I have to repeat the interface defintion?

See the attached source file for more info.

The necessity to repeat the interfaces is a minor irritation where there are only 2 or 3 of them. But it's more of a pain when the numbers grow or are even unknown at the time of compilation.

I think that I want a function type associated with a particular function signature. I can then declare several pointers pointing to functions of the same type. (You may guess that I come from a C++ background.)

Is there any way in FORTRAN of doing something similar?

I can see that one solution is to maintain just one function pointer and one interface and call GetProcAddress whenever necessary. But this will have some performance cost.

A development of that may be to keep the results of GetProcAddress in an integer array and assign the relevant element to the function pointer each time.

Any comments?

0 Kudos
7 Replies
Steven_L_Intel1
Employee
739 Views
If you wish to be able to refer to the functions by different names, then you need an interface for each. If you're doing this a lot, the array of integers makes sense - just make sure that the kind is INT_PTR_KIND().
0 Kudos
Jugoslav_Dujic
Valued Contributor II
739 Views
dmw@hrwallingford.co.uk:

Do 2 pointers to 2 functions require 2 interfaces?

Do 3 pointers to3 functions require3 interfaces?

I think that I want a function type associated with a particular function signature. I can then declare several pointers pointing to functions of the same type. (You may guess that I come from a C++ background.)



...but no, you don't have to have 2 interfaces to 2 functions, as long as they have the "same" signature. One will suffice, and the interface will kind of denote a "signature". IOW, you may write (untested):

INTERFACE
SUBROUTINE S1(x,y,z)
integer:: x
real:: y,z
END SUBROUTINE
END INTERFACE
POINTER(P1, S1)
POINTER(P2, S1)

But, due to the "dual" nature of Cray pointer, you must take care to switch the pointers accordingly each time, i.e.

P1 = GetProcAddress(..."Func1")
CALL S1(a,b,c)
P2 = GetProcAddress(..."Func2")
CALL S1(a,b,c)

which kind of defeats the purpose Open-mouthed smiley [:-D]. But you may store the pointers P1... P2 in an array.

0 Kudos
dmw
Beginner
739 Views

Thank you for your answer which helps to clarify my mind.

I didn't really want different names for my functions. I wanted different pointers to the same function interface and to invoke the function via its pointer.

But I'll accept that FORTRAN doesn't do it that way

Thank you for the advice re INT_PTR_KIND.

0 Kudos
dmw
Beginner
739 Views

Thank you for your answer which points to the solution towards which I was approaching.

I needed the assurance of 2 experts, such as Steve and yourself, that there was no other language feature which I was missing.

See also my reply to Steve.

Thanks again.

0 Kudos
Steven_L_Intel1
Employee
739 Views

You can't have two POINTER statements for the same function/interface name.

t.f90(8) : Error: Multiple declaration of a pointee. [S1]
POINTER(P2, S1)
------------^

0 Kudos
Jugoslav_Dujic
Valued Contributor II
739 Views
On retrospect, such limitation is logical, as an ambiguity would arise; my bad.

Of course, multiple different functions still can be called through a single (pointer, pointee) interface, provided that the pointer is reassigned appropriately before each instance.
0 Kudos
aliho
Beginner
739 Views

Something like that

INTERFACE
SUBROUTINE S1(x,y,z)
integer:: x
real:: y,z
END SUBROUTINE
END INTERFACE
POINTER(PTR_S1, S1)
INTEGER*4 PTR(:)


PTR(1) = GetProcAddress(..."Func1")
PTR(2) = GetProcAddress(..."Func2")...

PTR_S1=PTR(1)
CALL S1(a,b,c)
PTR_S1=PTR(2)
CALL S1(a,b,c)

...

0 Kudos
Reply