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
7
votes
3 answers

Fortran function returning allocatable array

Let me consider a function returning an allocatable array. Should the array variable holding the result (outside the function) be allocated before an assignment? Consider, e.g., the following program program mem implicit none interface …
francesco
  • 7,189
  • 7
  • 22
  • 49
6
votes
1 answer

Reading allocatable arrays from namelists

I am using GNU Fortran (GCC) 4.8.2 I want to read allocatable arrays from a namelist. But I don't know in advance how many elements have to be read into the allocatable array, so I cannot allocate it before to read the namelist. This is my…
5
votes
1 answer

Fortran (re-)allocation on assignment and gfortran warnings

A simple code: program main integer, allocatable :: A(:,:) integer :: B(3,4) B=1 A = B !A will get allocated, with the same shape and bounds as B end program main Compiling the above code with: gfortran-8 -std=f2008 -fcheck=all -Wall…
5
votes
1 answer

Internal memory representation of Fortran allocatable

I'd like to know what is the internal memory representation of a fortran allocatable array. I understand this a bit more complex than a raw pointer, since shape and ranks must be stored as well. I also guess it's implementation dependent, since I…
Regis Portalez
  • 4,675
  • 1
  • 29
  • 41
5
votes
1 answer

Fortran Allocatable Array Member of a User-Defined Type

I am stuck with segmentation fault at an allocatable array memberof a derived type in the following simple program. This segmentation fault occurs only on one machine (with Intel Fortran 14.0.3 on openSUSE) but not on the other machine (with Intel…
norio
  • 3,652
  • 3
  • 25
  • 33
4
votes
1 answer

Array bounds with 0-sized array in Fortran

When allocating zero-sized arrays in Fortran, I am getting counterintuitive behavior. This code: program test_zerosized implicit none integer, allocatable :: a(:),b(:) allocate(a(0)) print *, ' a lower bound = ',lbound(a,1) print *, ' a…
Federico Perini
  • 1,414
  • 8
  • 13
4
votes
1 answer

Read an allocatable string with a namelist in Fortran

Since Fortran 2003 is it possible to work with variable length character strings. Instead of working in an archaic way and declaring a constant string length I would like to read the character strings of my namelist dynamically. Consider the…
4
votes
2 answers

Fortran: Choosing the rank of an allocatable array

I am trying to write a program where I want the allocatable array A to be of either rank 1, 2, or 3, depending on my input at run-time. I want to do this since the subsequent operations on A are similar, and I have defined in a module an interface…
Lun
  • 43
  • 3
3
votes
2 answers

intent(out) and allocatable Fortran arrays: what is really done?

I read on many posts on Stack Overflow that an allocatable array is deallocated when it is passed in a subroutine where the dummy argument is intent(out). If I consider the following code : program main real, dimension(:), allocatable :: myArray …
Stef1611
  • 1,978
  • 2
  • 11
  • 30
3
votes
2 answers

Fortran doesn't keep lower/upper array bounds after copy to another allocatable array

This doesn't work program main implicit none integer :: nx = 3 integer :: ny = 5 integer :: nz = 8 real, allocatable, dimension(:,:,:) :: A real, allocatable, dimension(:,:) :: B allocate(A(nx,0:ny,nz) ) ! ...do something…
3
votes
1 answer

Unable to print allocated status of a allocatable inside a derived type

I would like to know why this code returns error in the last print. With gfortran 7.4.0 fails but with ifort 18.0.3 works well. program test implicit none type :: syntax integer, allocatable :: f(:) end type type(syntax), allocatable ::…
franpena
  • 151
  • 11
3
votes
1 answer

How to handle Fortran global allocatable variables in a module across subroutines

I have the following module with an allocatable variable which is defined in the module, allocated in a subroutine, and then also used in a second subroutine called by the first subroutine. In this situation do I have to pass the variable to the…
Herman Toothrot
  • 1,463
  • 3
  • 23
  • 53
3
votes
1 answer

Designing a derived type with array components

I have struggled to find any concrete information where designing a derived type is concerned. I think the best way to discuss this is through a couple of options. I have made up some sections of code with different applications of the derived type.…
3
votes
2 answers

Pointer to derived type that contains allocatable array

Generally speaking I want to rename allocatable variables in a derived type that are passed through subroutine arguments. Writing everything with 'derived%type_xx' is not so pleasant. Besides, I don't want to spend extra memory on copying the values…
Ruizhi
  • 87
  • 10
3
votes
1 answer

Derived type access time vs arrays access time

I have a question related to access times of multidimensional arrays or derived types. I wrote an algorithm which works quite well. However, the main part of this algorithm uses % to reference some data through different types. For example here is…
K Kolasinski
  • 340
  • 2
  • 11
1
2 3 4