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

TYPE(X), DIMENSION(:), ALLOCATABLE :: U ?

ratel__gilles
Beginner
871 Views

I am interested createan array of TYPE
But I don't understand how to.

Here a small example - not ok because I don't understand exactly
Suggestions ? (Thanks)

MODULE mod_TypeAllocate

IMPLICIT NONE

TYPE :: STRUCT
CHARACTER(LEN=2) :: CC
INTEGER, DIMENSION(2) :: II
REAL , DIMENSION(2) :: RR
END TYPE STRUCT

TYPE INFO
CHARACTER (LEN=12) :: NAME
TYPE(STRUCT) :: VAL
END TYPE INFO


CONTAINS
FUNCTION GETINFO(nv)
IMPLICIT NONE

INTEGER :: nv
TYPE(INFO), DIMENSION(:), POINTER :: GETINFO ! ??? allocatble, pointer ...

INTEGER :: istat

ALLOCATE(GETINFO(nv),STAT=istat)

GETINFO(1)%NAME = 'FIRST'
GETINFO(1)%VAL%CC = 'CC'
GETINFO(1)%VAL%II = 5
GETINFO(1)%VAL%RR = 99.99

END FUNCTION GETINFO

END MODULE mod_TypeAllocate

PROGRAM PRGTEST

USE mod_TypeAllocate
IMPLICIT NONE

TYPE(INFO), DIMENSION(:,:), POINTER :: VALS ! ??? allocatble, pointer ...

VALS = GETINFO(3)

END PROGRAM PRGTEST

0 Kudos
6 Replies
mecej4
Honored Contributor III
871 Views
All that needs to be done is to make sure that variables are allocated and of the proper type and shape before assigning values to them. For example, in the main program use

[fortran]   TYPE(INFO), DIMENSION(:), POINTER :: VALS   ! ??? allocatble, pointer ...
   ALLOCATE(VALS(3))
   VALS = GETINFO(3)
[/fortran]
In your version, you tried to assign a value to a dangling pointer variable (no allocation yet) and declared VALS to be a two dimensional variable to which you tried to assign a one-dimensional array.
0 Kudos
IanH
Honored Contributor III
871 Views
Careful - if GetInfo returns a POINTER then line three of mecej4's code is a memory leak. In that case ditch line two and make line three pointer assignment or (and this second option is much, much "safer") use ALLOCATABLEs for both the function result and the variable being assigned to in the code that references the function (in that case, if you compile with /standard-semantics you can also ditch the allocate statement before the function reference).
0 Kudos
ratel__gilles
Beginner
871 Views

Yes

But if I don't know exactly what is GETINFO. (IF module is a compiled module, binary only - no code)
If the code allocate, by example:

ALLOCATE(GETINFO(2*nv),STAT=istat)

I would function GETINFO allocate space and return values without program PRGTEST allocate variable VALS
0 Kudos
ratel__gilles
Beginner
871 Views
I thought that the function GetInfo could allocate space and return a pointer at this space.

My program PRGTEST just specify shape ( a one dimensionarray, (:),of type INFO )for variable VALS and function GETINFO allocate an array of size nv of type INFO.


Perhaps an INTERFACE statement is required (?)
0 Kudos
jimdempseyatthecove
Honored Contributor III
871 Views
In the module you are allocating a 1D array of INFO objects to a pointer and returning this pointer.
In PROGRAM you declare VALS as a 2D array of INFO objects.
In PROGRAM you use "VALS = GETINFO(3)" which states take the contents of the 1D array of INFO objects (as pointed to by the return from GETINFO) and copy it into the array VALSdescribed by the descriptor for VALS. VALS has a different shape as well as it is not allocated.

PROGRAM should declare VALS as a 1D pointer then use the pointer assignment "VALS => GETINFO(3)" to an unallocated VALS. You will not have a memory leak using this technique.

Did you want an array of pointers to 1D arrays of INFO objects?

Jim Dempsey
0 Kudos
mecej4
Honored Contributor III
871 Views
> (#3) But if I don't know exactly what is GETINFO. (IF module is a compiled module, binary only - no code)

If that is true, then your first efforts should be devoted to finding from the author of GETINFO what the function type and calling conventions are. Once that information is set you can go about writing suitable interfaces and calls.

Failing that, you will have to investigate all possible interfaces to GETINFO, write test programs and check the results. This is time consuming; some alternatives may slip through the cracks, and the task may require facilities not available to you. That is why this should be a last resort, and it may very well turn out that it is going to be better to discard the only-binary-code routine of uncertain pedigree and rewrite your own version with the desired functionality.

Any help that you can get here is going to suffer from the handicap that the problem is ill-defined and, therefore, there are a number of tentative solutions that may be tried but with no guarantee of success.


0 Kudos
Reply