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

Dynamic Arrays Compatibility (VB and VF)

omem84
Beginner
857 Views
Hi. I am having some problems in passing dynamic arrays between VB and VF. In VF I made some operationsin the arrays but when they are returned to VB the values are really weird. So I need some help, please.
VISUAL BASIC CODE
Option Explicit
Dim D1 As Long, D2 As Long, M() As Single, R As Long, C As Long
Private Sub Command1_Click()
Cls
D1 = CLng(Text1(0).Text)
D2 = CLng(Text1(1).Text)
ReDim M(1 To D1, 1 To D2)
Call OPERACION(D1, D2, M(1, 1))
For R = 1 To D1
For C = 1 To D2
Print M(R, C)
Next C
Next R
End Sub
Module 1
Option Explicit
Declare Sub OPERACION Lib "OPERACION.dll" (D1 As Long, D2 As Long, M As Single)

FORTRAN CODE
SUBROUTINE OPERACION(D1, D2, M)
IMPLICIT NONE
!DEC$ ATTRIBUTES DLLEXPORT::OPERACION
!DEC$ ATTRIBUTES ALIAS: "OPERACION" :: OPERACION
INTEGER D1, D2
REAL, ALLOCATABLE :: M(:,:)
INTEGER R, C
ALLOCATE (M(D1,D2))
DO R = 1, D1
DO C=1, D2
M(R,C) = 0
END DO
END DO
DO R= 1, D1
DO C= 1, D2
M(R,C) = R+C
END DO
END DO
END SUBROUTINE OPERACION
I dont know whats wrong, when I pass static arrays I dont have problems. Probably something change for dynamic arrays, so if someone can tell me, I will appreciate it.
0 Kudos
4 Replies
TimP
Honored Contributor III
857 Views
Your textbook on Fortran 95 should tell you that allocatable arrays are deallocated automatically on exit from the subroutine where they are allocated, unless the SAVE attribute is specified.
0 Kudos
omem84
Beginner
857 Views

Ok, Tim 18, thank you for your answer. You are right I forgot that detail butnow when I put the attribute SAVE appears an error "The save attribute conflicts with other declarations "

I dont know if you can tell me whats wrong now. Or another suggestion.

0 Kudos
Steven_L_Intel1
Employee
857 Views
Tim's answer, while correct on its own merits, is not relevant to your program. In your code, M is a dummy argument which does not get deallocated automatically and for which SAVE is inappropriate.

The problem you have is that you are assuming that VB dynamic arrays are interchangeable and passable as arguments to Fortran assumed-shape arrays. They are not. The two languages use entirely different mechanisms for describing arrays and dealing with allocation.

It is a rather complicated thing to have Fortran allocate or reallocate an array passed from VB. It's somewhat complicated just to access a VB multidimensional array in Fortran, but it can be done. If you look at the MixedLanguageVB.NET-Safearrays sample included here you'll see how such arrays can be accessed. My advice would be to do the allocation in VB if possible and just have Fortran access the data. You'll need to read up on SafeArrays and their use.
0 Kudos
omem84
Beginner
857 Views
Steve, thank you very much for your answer. So it wasn't so easy, I'm glad. I am a beginner and it was just a curiosity. I will check the sample you told me.
Muchisimas gracias
0 Kudos
Reply