Search code examples
fortranallocatable-array

Access violation when writing to a Fortran allocatable array


Program Main

Implicit None
Integer, Parameter :: iwp = SELECTED_Real_KIND(15)
Integer, allocatable :: Num(:)

Num(1)=1

......

End Program Main

When I use allocatable to define a void array 'num' and then run the program, it reveals that error as below

(1)"First-chance exception at 0x00B21147 in Index.exe: 0xC0000005: Access violation writing location 0x00000004"

(2)"If there is a handler for this exception, the program may be safely continued"

enter image description here


Solution

  • Array Num needs to be allocated first. For example

     allocate(Num(1:10))
    

    end then you can use indexes from 1 to 10 for setting values and reading them.