Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

allocatable array problem

Status
Not open for further replies.

dongli

Technical User
Aug 30, 2009
9
CN
Hi,

I made a small test program for allocatable array, as following

Code:
real, allocatable :: a(:)

allocate(a(2))

a(1) = 1.0
a(2) = 2.0

a(3) = 3.0    ! This can be accessed!!!

write(*, *) a(1), a(2)
write(*, *) a(3)
[\code]

The result is:

[code]
   1.00000000000000        2.00000000000000     
   3.00000000000000
[\code]
 
Hi dongli,

I tried it and have similar behaviour.

Consider the following code
alloc_array.f95
Code:
[COLOR=#2e8b57][b]real[/b][/color], [COLOR=#2e8b57][b]allocatable[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:) :: a

[COLOR=#804040][b]allocate[/b][/color](a([COLOR=#ff00ff]2[/color]))
a([COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1.0[/color]
a([COLOR=#ff00ff]2[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]2.0[/color]
a([COLOR=#ff00ff]10[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]3.0[/color] [COLOR=#0000ff]! This should not be accessed[/color]

[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'After allocation:'[/color]
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'allocated(a)='[/color], [COLOR=#008080]allocated[/color](a)
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'size(a)='[/color], [COLOR=#008080]size[/color](a)
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'a(1)='[/color], a([COLOR=#ff00ff]1[/color]), [COLOR=#ff00ff]', a(2)='[/color], a([COLOR=#ff00ff]2[/color]), [COLOR=#ff00ff]', a(10)='[/color], a([COLOR=#ff00ff]10[/color])

[COLOR=#804040][b]deallocate[/b][/color](a)
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'After deallocation:'[/color]
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'allocated(a)='[/color], [COLOR=#008080]allocated[/color](a)
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'size(a)='[/color], [COLOR=#008080]size[/color](a)
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'a(1)='[/color], a([COLOR=#ff00ff]1[/color]), [COLOR=#ff00ff]', a(2)='[/color], a([COLOR=#ff00ff]2[/color]), [COLOR=#ff00ff]', a(10)='[/color], a([COLOR=#ff00ff]10[/color])
[COLOR=#a020f0]end[/color]
When I compile it with g95 then I get
Code:
$ g95 alloc_array.f95 -o alloc_array

$ alloc_array
 After allocation:
 allocated(a)= T
 size(a)= 2
 a(1)= 1. , a(2)= 2. , a(10)= 3.
 After deallocation:
 allocated(a)= F
 size(a)= 2
 a(1)= 1. , a(2)= 2. , a(10)= 3.

You see, with g95 after allocating the array with size 2 , i.e. when the function allocated() returns True, then a(10) could be accessed and printed.
But worse is, that it's possible too after deallocating the array, i.e. when allocated() returns False.

With gfortran the above code compiles too, but when I try to run it, it fails (MinGW on Vista).

So I think that the behaviour is not defined in Fortran standard and it depends from the compiler used.
It's on the programmer to control how you access the array. You have therefore the functions allocated() and size() available.

I found something similar here
 
Thanks for explanation. I will keep it in mind when programming! : )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top