Questions tagged [allocatable-array]

This tag is about the use of allocatable arrays in Fortran. Such arrays can have their bounds (shape) varying at run time. Questions using this tag should usually have the more general fortran tag also.

In Fortran, the bounds (and shape) of an allocatable array are determined when it is allocated. Subsequent redefinition or undefinition of any entities in the bound expressions does not affect the array specification.

If the lower bound is greater than the upper bound, that dimension has an extent of zero, and the array has a size of zero. If the lower bound is omitted, it is assumed to be 1.

When an array is allocated, it is definable. If you try to allocate a currently allocated allocatable array, an error occurs.

60 questions
3
votes
1 answer

seg fault when sending derived type data with allocatable array in mpi

I'm trying to send a derived type data with allocatable array in mpi ad got a seg fault. program test_type use mpi implicit none type mytype real,allocatable::x(:) integer::a end type mytype type(mytype),allocatable::y(:) …
Huy Le
  • 33
  • 3
2
votes
0 answers

Fortran function returning allocatable array with non-trivial bounds

As per object, I'm struggling understanding the logic behind functions returning allocatable arrays. I like this construct because its clarity compared to subroutines, and because pure functions in fortran are an excellent way to write clean,…
2
votes
1 answer

Access extended type components in a SELECT TYPE construct

I'm trying to build an allocatable array with polymorphic elements. A minimal example is the following : program PolyArray implicit none type basetype integer :: ib end type basetype type, extends(basetype) :: exttype1 real :: r1 end…
2
votes
2 answers

Unlimited polymorphic dummy argument with allocatable or pointer attributes?

In the project I'm working on, I find myself frequently needing to resize arrays of objects as new objects are created and old ones destroyed. This happens with numerous different derived types throughout the code, most of which have no…
2
votes
0 answers

Why does gfortran's runtime bounds check not work when passing allocatable arrays as automatic arrays to subroutines with wrong shape?

I compile the following program with gfortran -g -fcheck=all -Wall bug.F90: program main implicit none real, dimension(:), allocatable :: arr allocate (arr(5)) ! should fail, but happily writes out of bounds call…
letmaik
  • 3,348
  • 1
  • 36
  • 43
2
votes
1 answer

Distinguishing generics in Fortran by other than type/kind/rank

I make huge use of non-1 indexed ALLOCATABLE arrays, whose actual lower (and thus upper) bounds I want to be known for the procedures they're given to as IN/INOUT args (so I declare these dummy arguments as deferred-shape arrays to make them be…
Enlico
  • 23,259
  • 6
  • 48
  • 102
2
votes
2 answers

Fortran function returning unallocated array causes segmentation fault

I'm struggling with some Modern Fortran wrappers to some MPI scatter/gather routines. I am trying to have a wrapper interface that only has an array on input and returns the MPI-operated result on output, for several derived types, doing something…
2
votes
0 answers

Threadprivate allocatable performance issues with OpenMP and Fortran

I have a parrallel part of a code which uses a THREADPRIVATE ALLOCATABLE array of a derived type which, in turns, contains other ALLOCATABLE variables: MODULE MYMOD TYPE OBJ REAL, DIMENSION(:), ALLOCATABLE :: foo1 REAL, DIMENSION(:),…
Teloze
  • 279
  • 2
  • 8
2
votes
2 answers

Can An Allocatable Intent(inout) Argument Be Optional?

I have problem trying to define a subroutine, whose argument contains an allocatable, optional, intent(inout) variable shown below. The code compiles fine, but get runtime error of "Segmentation fault - invalid memory reference". Subroutine…
Ruizhi
  • 87
  • 10
2
votes
1 answer

Passing a user defined data type allocatable array

I can define a user defined data type with allocatable array as its data type. Allocation works perfectly while we are still in the same subroutine. But i don't know how to pass this type of user defined data type as a subroutine argument. Intel…
2
votes
1 answer

Allocate array from array of sizes

Let's say I have an array of dimensions declared like this: integer, dimension(5) :: dims dims = (/ 5, 6, 7, 8, 9 /) How can I most simply use this array to allocate another array, using the elements of dims to specify the size of the respective…
1
vote
1 answer

Fortran derived-type parameter with allocatables

I have a derived-type with pointer (or allocatable) component type Tmy real, dimension(:), pointer :: vals integer :: nVals end type Tmy And I want to create a parameter, e.g. to pass it to a subroutine as intent(in): type (Tmy), parameter,…
Roux
  • 435
  • 3
  • 12
1
vote
2 answers

How to have an allocatable array of variable rank (number of dimensions)?

I have two programs : 2D and 3D versions. For example, this is a simplified 2D version : program main integer, parameter :: nDim = 2 integer :: size real, dimension(:,:,:), allocatable :: myArray size=4 ! Normally read from a file …
Stef1611
  • 1,978
  • 2
  • 11
  • 30
1
vote
1 answer

Reset (deallocate / nullify) a Fortran allocatable array that has been corrupted

When a situation such as described in Incorrect fortran errors: allocatable array is already allocated; DEALLOCATE points to an array that cannot be deallocated happens (corrupted memory leaves an allocatable array that appears allocated but does…
Jellby
  • 2,360
  • 3
  • 27
  • 56
1
vote
1 answer

How to solve 'Fortran runtime error: I/O past end of record on unformatted file'?

Now I have one 1024 * 1024 * 1024 array, whose dtype is float32. Firstly I save this array to one file in the format of '.bigfile'. And then I convert this bigfile to the Fortran unformatted file by running the code as below. with…