Deallocation of arrays passed as c_ptr

Maybe I wrote too short or too fast, in no case did I mean he needed target for allocation. He needs both, allocatable and target if he wants to own the data by allocating in the Fortran shared object, be able to point to it, and be able to retrieve its address latter on for destroying it.

He’s module as is, has (AFAIK) unknow behavior and its (seems to me) dangerous Compiler Explorer

program main
    use inter

    type(c_ptr) :: array
    integer, pointer :: ptr(:)

    array = gen_pointer(10,1)
    call c_f_pointer(array,ptr,[10])
    print *, ptr

    call dealloc_pointer(array,10)
    print *, ptr
end program

ifort

Program stdout
           1           1           1           1           1           1
           1           1           1           1
Program stderr
forrtl: severe (173): A pointer passed to DEALLOCATE points to an object that cannot be deallocated
Image              PC                Routine            Line        Source             
output.s           0000000000404619  Unknown               Unknown  Unknown
output.s           000000000040415D  Unknown               Unknown  Unknown
libc.so.6          00007FD2CB629D90  Unknown               Unknown  Unknown
libc.so.6          00007FD2CB629E40  __libc_start_main     Unknown  Unknown
output.s           0000000000404075  Unknown               Unknown  Unknown

gfortran

Program stdout
           1           1           1           1           1           1           1           1           1           1
        6976           0  1192534655   432068720           1           1           1           1           1           1
1 Like