FORTRAN pointer encompassing multiple arrays

流过昼夜 提交于 2020-08-20 07:46:59

问题


I'm working on a project where I have a number of arrays of the same size in the 1st, 2nd and 3rd dimension, although the sizes may vary in the 4th dimension.

I would like to group these arrays by constructing a pointer which concatenates these arrays.

To make this less abstract, let's say I have 2 arrays:

A (size: N1 x N2 x N3 x N4a)
B (size: N1 x N2 x N3 x N4b)

in previous versions of the project these arrays where copied to an array C of size N1 x N2 x N3 x (N4a + N4b) which would then be passed to a subroutine to perform ffts on this array.

I would like to avoid this copying operation and construct a pointer p which would contain the same data as the array C in the previous version but without the explicit copying and additional memory allocation.

Is this possible in Fortran?


回答1:


No. A pointer cannot point across two otherwise independent objects like that at the same time.

Depending on your situation, what might be workable is to start with an array that is of dimension (N1,N2,N3,N4a+N4b) and then make A and B associated (pointer, storage or argument) to the relevant parts of that initial big array in some way.

REAL, TARGET :: c(N1,N2,N3,N4a+N4b)
REAL, POINTER :: a(:,:,:,:)
REAL, POINTER :: b(:,:,:,:)

a => c(:,:,:,:n4a)
b => c(:,:,:,n4a+1:)
! Go forth and do things with a and b.

! Then later do things with c.

In the dark times, before Fortran had dynamic memory allocation of any sort, this sort of one-array-to-rule-them-all that then got divvied out was in common usage.




回答2:


In the old times you would place them in a commn block and pass the first one as assumed size. The sequence association rules makes it work. That would only work if the sizes were fixed.

Note: please don't downvote just because you don't like old programs, but show that it does not work.



来源:https://stackoverflow.com/questions/19990658/fortran-pointer-encompassing-multiple-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!