1

My IDE is: Visual Studio 2010 ( Intel Parallel XE 2011 )

This is test code:

program test

  implicit none

  character(:), allocatable :: glo_isp_gre
  character(*),   parameter :: glo_isp_aaa = "abcd!"  
  
  allocate( character( len(glo_isp_aaa) ) :: glo_isp_gre)

  glo_isp_gre = glo_isp_aaa

  print *, len(glo_isp_gre), len(glo_isp_aaa)

  deallocate( glo_isp_gre)

  print *, len(glo_isp_gre)   

end program test

The result of program execution is: 5 5 and 5

The length of glo_isp_gre is 5 after deallocation. Why?

Ian Bush
  • 6,996
  • 1
  • 21
  • 27
  • The linked question talks about `size` of an array, but exactly the same details hold for `len` of a character (and similar concepts). – francescalus Nov 29 '20 at 16:25
  • @francescalus About arrays I found a bunch of examples but I want to learn how I can use deallocate to free that variable for further allocation if I need it....i know ho to do it with implicit assignment. – Harry Kastorp Nov 29 '20 at 17:07
  • You _have_ deallocated it. You aren't allowed to use `len` once it's deallocated, so the answer you get is "junk": test with `allocated(glo_isp_gre)`. – francescalus Nov 29 '20 at 17:13
  • @francescalus I try with move_alloc to increase the length with 1 and I get a zero so just to make conclusion; I can use allocate and deallocate just like in case with arrays. Is that correct? – Harry Kastorp Nov 29 '20 at 17:22
  • Yes, `allocate` and `deallocate` work in the same way for all entities whether array or scalar, with deferred type (such as length) parameter or without. Once you've deallocated a deferred length character variable you can't ask about length just as you can't ask about the size of an array just deallocated. – francescalus Nov 29 '20 at 17:29
  • @francescalus Hm, now i think about this `character(:), allocatable :: glo_isp_gre(:)`. Deffered shape of array of deffered length character. I will try now optiones. – Harry Kastorp Nov 29 '20 at 17:44

0 Answers0