Fortran array of variable size arrays -
a simplified description of problem:
there maxsize people shopping in store. each of them has shopping list, containing price of items (as integers). using fortran arrays, how can represent shopping lists. shopping lists may contain any number of items (1, 10, 1000000000).
(note: actual problem far more complicated. not shopping.)
the lazy approach be:
integer :: array(maxsize, a_really_big_number)
however, wasteful, want second dimension variable, , allocate each person seperately.
the obvious attempt, doomed failure:
integer, allocatable :: array(:,:) allocate(array(maxsize, :)) ! compiler error
fortran seems require arrays have fixed size in each dimension.
this wierd, since languages treat multidimensional array "array of arrays", can set size of each array in "array of arrays" seperately.
here does work:
type array1d integer, allocatable :: elements(:) ! compiler fine this! endtype array1d type(array1d) :: array2d(10) integer :: i=1, size(array2d) allocate(array2d(i)%elements(sizeat(i)) enddo
if solution, guess use it. kind of hoping there way using intrinsic functions. having define custom type such simple thing bit annoying.
in c, since array pointer fancy syntax, can array of pointers:
int sizeat(int x); //function gets size in 2nd dimension int * array[maxsize]; (int x = 0; x < maxsize; ++x) array[x] = (int*)(calloc(sizeat(x) , sizeof(int)));
fortran seems have pointers too. tutorials have found "never use these ever" or similar.
you seem complaining fortran isn't c. that's true. there near infinite number of reasons why standards committees chose things differently, here thoughts:
one of powerful things fortran arrays can sliced.
a(:,:,3) = b(:,:,3)
is valid statement. this not achieved if arrays "arrays of pointers arrays" since dimensions along each axis not consistent (the case you're trying implement).
in c, there no such thing multidimensional array. can implement looks similar using arrays of pointers arrays, isn't multidimensional array since doesn't share common block of memory. can have performance implications. in fact, in hpc (where many fortran users spend time), multi-dimensional c array 1d array wrapped in macro calculate stride based on size of dimensions. also, dereferencing 7d array this:
a[i][j][k][l][m][n][o]
is bit more difficult type than:
a(i,j,k,l,m,n,o)
finally, solution you've posted closest c code you're trying emulate -- what's wrong it? note problem statement, more complex data-structure (like linked-list) might in order (which can implemented in c or fortran). of course, linked-lists worst far performance goes, if that's not concern, it's correct data structure use "shopper" can decide add more things "cart", if wasn't on shopping list took store.
Comments
Post a Comment