Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

behavior realloc-lhs function

pat97269
Beginner
248 Views
Hi ,
I wonder what is exactly the good behavior for a function y=f(y) which return a greater array:
1) When y is an allocatable array ,
2) When y is a type containing an allocatable array.
From my experiments,
for an allocatable array there is no error saying the return array is not the same size (even whit -C ) and the result is just copied up to the declared size of y. If i deallocate y in f then i got a segfault, which i can understand since it assume norealloc-lhs. ( this works with gfortran 4.7 so i believe they have enabled realloc-lhs by default)
For a type there is no error and the returned type has the good size and array, which i believe realloc-lhs is enabled for type. If i deallocate the array in the type in f then it gets reallocated at the good size. ( GCC give me a double free corruption here).
So finally, if assume no-realloc by default why not for type ? and should the compiler throw an error for the size mismatch instead of silently fill the array ? [fortran]program test implicit none ! real,pointer :: matrix(:,:),diagonal(:),base_array(:) ! allocate(matrix(5,5)) ! base_array => matrix type po double precision,dimension(:),allocatable :: p end type integer :: i double precision,dimension(:),allocatable :: p1p type(po) :: p1 allocate(p1%p(1)) do i=1,10 p1%p=1d0 p1=toto(p1) end do print *,'p',p1%p contains function toto(dd) result(y) !double precision,dimension(:),allocatable :: ddp,yp type(po) ::dd,y allocate(y%p(10)) !deallocate(dd%p) y%p=3d0 ! print *,'yp',yp end function toto end program test[/fortran]

0 Kudos
1 Reply
Steven_L_Intel1
Employee
248 Views
The default of norealloc-lhs assumes Fortran 90 rules which are inconsistent like that. Fortran 2003 changed the rule for an allocatable array on the left side of the assignment.
0 Kudos
Reply