Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29276 討論

Unhandled exception at 0x00401064 in Test.exe: 0xC0000005: Access violation reading location 0x00000020.

turkeydodo
初學者
1,643 檢視
How can I do for this dynamically allocating array? I want to allocate it in subroutine not in main program. Thanks!

I got the error message in the following code: Unhandled exception at 0x00401064 in Test.exe: 0xC0000005: Access violation reading location 0x00000020.


PROGRAM MAIN

REAL,ALLOCATABLE::M(:)

CALL NEW(M)

STOP

END


SUBROUTINE NEW(M1)

REAL,ALLOCATABLE::M1(:)

INTEGER i,N

N=100

ALLOCATE(M1(N))

do i=1,N

M1(i)=i

ENDDO

RETURN

END

0 積分
4 回應
anthonyrichards
新貢獻者 III
1,643 檢視
I compiled this in CVF and got

:\F90\allocatetest\allocatetest.f90(23) : Warning: In the call to NEW, actual argument #1 does not match the type and kind of the corresponding dummy argument.
CALL NEW(M)
-----------------^

when built and run, an access violation occurred that caused DevStudio to crash and exit.
mecej4
榮譽貢獻者 III
1,643 檢視
Requirement of the Fortran Standard: The caller of a procedure that takes allocatable arguments must provide an explicit interface for the procedure.

The main program has M declared as allocatable, but that is not enough to tell the compiler that subroutine NEW takes an allocatable argument. Indeed, it is possible to do the allocation in the main program and pass the argument to a subroutine; in that case, from the latter's point of view, there would be no distinction between an argument that was dynamically allocated and an argument that was allocated at compile time.
TimP
榮譽貢獻者 III
1,643 檢視
This needs a complete f90 interface (interface block, module subroutine, CONTAINS subroutine). Is your point that the compiler should tell you this explicitly? If you used ifort, you would have to be willing to set /gen-interfaces in hopes of getting such a message. I do find the documentation confusing, as ifort -help still says you must use both /gen-interfaces and /warn-interfaces.
Steven_L_Intel1
1,643 檢視
Actually, in current versions of ifort, /warn:interface is the option and /gen-interface is obsolete. It is also the default in newly created Visual Studio projects and has been since version 11.0.
回覆