写入 Fortran 可分配数组时发生访问冲突

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

当我使用 allocatable 定义一个空数组 'num' 然后 运行 程序时,它显示如下错误

(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"

数组Num需要先分配。例如

 allocate(Num(1:10))

end 然后你可以使用从 1 到 10 的索引来设置值和读取它们。