realloc 是 Sun 提供的函数,不是标准的。 
不管是VB的 Redim Preserve,还是 C++ 容器类的 resize() 成员。其底层的实现本质,都是重新分配,然后复制原来的值。 
 
Fortran需要自己去实现,而且由于Fortran没有模板类,所以每种类型都要写一遍,二维数组又要写一遍,三维还要写一遍。实际上,C++ 压根没有多维数组,VB Redim Preserve 也只能改变最后一维的大小,前面不能改变。 
静态编程语言,改变大小都很难。想要自由编程,可以选 Python 或者 matlab 这种动态编程语言。想怎么加怎么加。 
 
用预处理宏定义可以节省一点时间。 
 
[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode Program Main
  Integer , allocatable :: x(:)
  Real    , allocatable :: y(:)
  allocate(x(4),y(4))
  x = 3 ; y = 4
  write(*,*) x
  write(*,*) y
  call ReallocateInt( x , 3 )
  call ReallocateReal( y , 6 )
  write(*,*) x
  write(*,*) y
  
contains
#define TYPE_  Integer  
  Subroutine ReallocateInt( x , n )
    TYPE_ , allocatable :: x(:) , t(:)
    integer :: n , s
    call move_alloc(x,t)
    allocate(x(n))
    s = min(size(t),n)
    x(:s) = t(:s)
    deallocate(t)
  End Subroutine ReallocateInt
#define TYPE_  Real  
  Subroutine ReallocateReal( x , n )
    TYPE_ , allocatable :: x(:) , t(:)
    integer :: n , s
    call move_alloc(x,t)
    allocate(x(n))
    s = min(size(t),n)
    x(:s) = t(:s)
    deallocate(t)
  End Subroutine ReallocateReal
  
End Program Main 
 |