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

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

turkeydodo
Beginner
1,628 Views
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 Kudos
4 Replies
anthonyrichards
New Contributor III
1,628 Views
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.
0 Kudos
mecej4
Honored Contributor III
1,628 Views
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.
0 Kudos
TimP
Honored Contributor III
1,628 Views
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.
0 Kudos
Steven_L_Intel1
Employee
1,628 Views
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.
0 Kudos
Reply