Quantcast
Channel: Intel® Fortran Compiler for Linux* and macOS*
Viewing all articles
Browse latest Browse all 2583

Packing an allocatable derived type array gets messed up after deallocating the original array

$
0
0

Hello All,

When an allocatable derived type array (say container) is packed (using the intrinsic pack function) then the content of the resulting array (say packed_container) change when deallocating the original array. To clarify, in the following program the array packed_container is printed before and after deallocating container. Please let me know if you can reproduce the result. Up to now I don't have any suggestion for a workaround I would  appreciate any help on this. Many thanks in advance.

Kostas

PS: The code and it'output follow, compiled by ifort 14.0.2.144

module my_storage_type

implicit none

type storage1
  integer :: id=0
  integer, dimension(:), allocatable :: lowkey
end type storage1

end module my_storage_type


program compress_test

use my_storage_type

implicit none

type(storage1), dimension(:), allocatable :: container, cont_he
integer :: i

! inits
 allocate(container(4))

 container(1)%id = 2
 allocate(container(1)%lowkey,source=(/1,2,3,4/))

 container(3)%id = 5
 allocate(container(3)%lowkey,source=(/5,6,7,8/))
! ------

allocate(cont_he,source=pack(container,container%id/=0))

print *, " output before deallocating container"
print *, cont_he

deallocate(container)

print *, " output after deallocating container"
print *, cont_he


end program compress_test

produces the output:

  output before deallocating container
           2           1           2           3           4           5
           5           6           7           8
  output after deallocating container
           2           0           0           3           4           5
    11551136           0           7           8

 


Viewing all articles
Browse latest Browse all 2583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>