I know this is the sort of thing that everyone has to wrestle with when they first start wrestling with pointers; I'd be immensely grateful if someone could point ouf why it doesn't work and how to *make *it work. Consider the following code, a mini-version of what I'm trying to do for real. I'm using user-defined types with pointer components, so the storage can be sized and allocated at run-time. I'm trying to:
- use a first variable of such a type as working space,
- assign the result to a second variable of the same type,
- free up the storage allocated to the first
But as soon as I free the storage for the first variable, all the data in the second variable turns to garbage. The code below illustrates. My question is, is there a way to make the data in the second variable 'non-volatile'?
program more_fun_with_pointers
type may_contain
integer, pointer :: nuts (:) => null ()
end type may_contain
type (may_contain) brazil, cashew
integer nut
allocate (brazil% nuts (10), cashew% nuts (10))
do nut = 1, 10
cashew% nuts (nut) = nut
end do
brazil = cashew
deallocate (cashew% nuts)
stop
end program more_fun_with_pointers