Re: Efficient way to pass different shaped arrays?




"Peter" <sherwood@xxxxxxxxxxxxxx> wrote in message
news:1151528445.773612.191520@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm attempting to use an array argument of a subroutine for two
different purposes. One, which works fine, has the caller declare an
identical explicit shape array. The other, shown below, has the caller
(A) passing a small number of reals in the same memory space.
Subroutine B knows, via other arguments not shown, which kind of array
argument is being received.

In subroutine A:

REAL, DIMENSION(3) :: array
CALL B(array)

SUBROUTINE B(array)
REAL, DIMENSION(28,0:10001), INTENT(INOUT) :: array

Under CVF6, this worked as expected, but using IVF9, the compiler flags
an error because the extent of 'array' as a dummy argument for B
exceeds the size of the real argument 'array' in A; presumably this is
an error because B could write memory beyond the size of 'array'
declared by the caller.

So, my question is, what is a simple and efficient way to pass in the
3-element array? I don't really want to create a huge 28x10002 array in
the caller, and for efficiency it would be better not to add another
argument (subroutine B gets called many times).


Well, you could use an assumed-shape array. This would require an explicit
interface and also the ranks to be identical. Thus, it might look like:

REAL, DIMENSION(3,1) :: array ! dummy second dimension
CALL B(array)

SUBROUTINE B(array) ! in a module or as an internal procedure
REAL, DIMENSION(:, 0:), INTENT(INOUT) :: array

You can use the lbound and ubound intrinsics to find out the actual bounds
at run time.

This looks like legacy code, so maybe a more extensive makeover would be
useful.

Does that help?

Mike Metcalf


.



Relevant Pages

  • Efficient way to pass different shaped arrays?
    ... I'm attempting to use an array argument of a subroutine for two ... The other, shown below, has the caller ... passing a small number of reals in the same memory space. ... Subroutine B knows, via other arguments not shown, which kind of array ...
    (comp.lang.fortran)
  • Re: array verses ArrayList
    ... > I have a class that contains a collection of reference types. ... > needs to have a method that returns the collection to the caller. ... If the size cannot change, then no, an Array of MyObject is preferable. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: About Memory Allocation and function calls
    ... function(because the caller don't know the exact size). ... how should the caller access this array for future use? ... Allocated memory remains allocated until it is explicitly freed. ... the calling function has access to. ...
    (comp.lang.c)
  • About Memory Allocation and function calls
    ... function(because the caller don't know the exact size). ... how should the caller access this array for future use? ... I consider to use mallocto allocate memory for this array, ... return the pointer to this run time allocated memory back to the ...
    (comp.lang.c)
  • Re: out param question
    ... an arbitrary length array of bytes.... ... You allocate a block of memory with CoTaskMemAlloc, ... it in *pData and its length in *pSize. ... The caller eventually frees the ...
    (microsoft.public.win32.programmer.ole)