开发者

How to create a handle for a structure/array inside a FORTRAN routine?

I need to create an handle for an fairly complicated structure (here replaced with "real a(2)") in a subroutine and then only pass back the handle/pointer to the main routine. I also need to be able to create as many as of these structure that are needed. The problem is that the data structure will be deallocated once the pointer is returned to main. Here is my attempt:

  program main
  implicit none

  integer i, j
  real a(2) ! This can be a complicated structure

  a=(/1.1,2.2/)
  ca开发者_如何学Goll myalloc(a,i)

  a=(/3.1,4.2/)
  call myalloc(a,j)

  call myprint(i)      
  call myprint(j)

  stop
  end

c-----------------------------------

  subroutine myalloc(a,i)
  implicit none

  real, pointer :: i(:) ! If I add "save" attribute I will get an error
  real, target :: a(2)

  allocate(i(2))
  i => a

  return
  end 

c--------------------------------

  subroutine myprint (i)
  implicit none

  real, pointer :: i(:)

  print *, i

  return
  end

c---------------------------------

The result is some garbage values: 3.93764868E-43 1.40129846E-45

Or I would get: At line 41 of file test.f (unit = 6, file = 'stdout') Internal Error: list_formatted_write(): Bad type

Any help would be highly appreciated.


There is so much wrong with this code it is almost too difficult to answer constructively.

If I understand what you are trying to do, the subroutine myalloc is supposed to act like a copy constructor of sorts - allocate memory onto a supplied pointer and then copy the contents of an initializing argument onto the memory allocated. So the key error in your myalloc is these two lines:

  allocate(i(2))
  i => a

Here you first allocate memory to the pointer i, then assign i to point to a. Why allocate i at all in this case? I presume this is closer to what you are trying to do:

subroutine myalloc(a,b,n)
  implicit none

  real, dimension(n) :: a
  real, pointer,dimension(:) :: b
  integer :: n

  allocate(b(n))
  b = a ! This copies the contents of the array a to the allocation for b

  return
end 

Then in your main program there are a number of inexplicable things. Why are i and j declared as integers? Surely they must be pointers to real arrays, if the intention is to pass them to your myalloc, perform the allocation/copy operation on them, then print them? If that is the case, then changing your main to something like this should be closer to what it seems you are trying to do:

program main
    implicit none

    interface
        subroutine myalloc(a,b,n)
        real,dimension(n) :: a
        real,pointer,dimension(:) :: b 
        integer :: n
        end subroutine myalloc

        subroutine myprint(i)
        real,pointer,dimension(:) :: i
        end subroutine myprint
    end interface

    real,pointer,dimension(:) :: i, j
    real :: a(2)

    a=(/1.1,2.2/)
    call myalloc(a,i,2)

    a=(/3.1,4.2/)
    call myalloc(a,j,2)

    call myprint(i)
    call myprint(j)

    stop
end

With these changes, you should get this when run:

$ ./pointer
   1.100000       2.200000    
   3.100000       4.200000    

Is that closer to what you were expecting as output?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜