Fortran increase dynamic array size in function

前端 未结 3 1063
滥情空心
滥情空心 2020-12-30 13:28

I need a variable size array in Fortran. In C++ I would use vector. So I have a function like

integer function append(n, array, value)
  integer, pointer, d         


        
3条回答
  •  不思量自难忘°
    2020-12-30 13:53

    The answer by IRO-bot is the correct approach for Fortran 90. If you can limit yourself to compilers that support the Fortran 2003 MOVE_ALLOC intrinsic (included in gfortran since the 4.2 release), you can avoid one of the copies. That is, increasing the size of an array by a factor of 2 can be written as

    
    allocate(tmp_arr(2*size(array)))
    tmp_arr(1:size(array)) = array
    deallocate(array)
    move_alloc(tmp_arr, array)
    ! tmp_arr is now deallocated
    

提交回复
热议问题