1

I have the following very simple code. What I don't understand is after I deallocate my array x, I would assume that my pointer ptr no longer can be dereferenced. However, If you run the program you will see that the print statement will correctly give the values of x, and likewise give its size even though I have deallocated the array before I dereference the pointer.

Is this because my x values are still loaded on the higher memory levels (L1,L2 etc) and would need more time to refresh its state?

  program ptr_deall

  implicit none

  real, allocatable,target :: x(:)
  real, pointer :: ptr(: )


  allocate(x(100))

  x = 100

  ptr => x
  deallocate(x)
  print*, ptr,size(ptr)

  end program
ATK
  • 1,296
  • 10
  • 26
  • As the linked question details, what you have is not valid Fortran. Because `ptr`'s target is deallocated not through the pointer, the pointer status becomes undefined and you cannot later reference it the way you do. (When I run this I expect to see a run-time error instead of the behaviour you see: I compile with error checking much of the time.) If you really want detail on why you see particular results then we'll need more detail on your setup. – francescalus Oct 14 '19 at 09:25
  • 2
    FWIW, I think this is a much cleaner example than the linked question. We'll have to look at some sort of merge. – francescalus Oct 14 '19 at 09:32
  • I compile with `-check all -debug full -O0` using the latest intel ifort version 19.0.5. I fully understand that this is not allowed. However, when I say deallocate(x) the memory should be freed and hence, the pointer should not be able to tell me what the content of x is. So why is it still able to tell me what the content of x is. – ATK Oct 14 '19 at 11:31
  • Okay I see. The compiler does not have the job to release the memory and hence, the pointer may still be pointing to the memory. Although, we can probably never know when the memory is actually freed, and thus this should be strictly avoided – ATK Oct 14 '19 at 11:33
  • Yes. Because you haven't written a valid Fortran program the compiler isn't required to do any particular thing. If the memory hasn't been "cleared" (and the compiler is allowed to leave it populated) then you may see the contents. The compiler also doesn't have to reassociate/nullify the pointer or complain that you've dereferenced it when it's pointer association status is undefined. As you say, avoid doing this. – francescalus Oct 14 '19 at 11:46
  • @francescalus, how can one go about to merge/modify these questions? Completely agree with you and would be happy to assist if required/necessary – jbdv Oct 14 '19 at 12:43
  • @jbdv, merging proper requires a mod flag, but I don't know if these questions are similar enough. It may well be that we reopen this question and close the other as a duplicate of this one (suitably answered). I'm reluctant to do that latter, as there's not so much difference between the two. A [meta] question may be appropriate. – francescalus Oct 14 '19 at 13:36

0 Answers0