Is there a difference between
integer, intent(in) :: n
integer, dimension(:), allocatable :: a
allocate(a(n))
and
integer, intent(in) :: n
integer, dimension(n) :: a
In which situation would we use the first version? Perhaps I misunderstood allocatable array, is the second version even an allocatable array?
The second case indeed doesn't have a
allocatable. It is, however, an automatic object.
Ignoring the practical differences in memory use at the implementation level, there is a big difference. Yes, each a
is (assuming things not explicitly stated in the question) a local variable which is, after the allocate
and the automatic creation, of size n
, but in the first case it is allocatable. It can be deallocated, reallocated (perhaps to a different size), and deallocated again. And so on.
The automatic object (second case) cannot be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With