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

Not keeping an AUTOMATIC variable value on re-entry(?)

yair999
Beginner
476 Views

I am trying to demonstrate that an AUTOMATIC variable do not retain its value upon re-entry into a subroutine. The following sample DOES retain 'x' value which prints as 1.000000upon the 2nd entry into 'sub'. How can I demonstrate AUTOMATIC vs. SAVE attribute with a simple program?

call sub

call sub

end

subroutine sub

real, automatic :: x

print *,x

x=1.

end

Thanks,

Yair

0 Kudos
2 Replies
TimP
Honored Contributor III
476 Views
The automatic storage is not purged upon leaving sub. It is available for re-use. If you call another subroutine between, setting different values on the stack, you may find the data have been over-written.
0 Kudos
yair999
Beginner
476 Views

Your hint did make the following new sample prove the point.

Now x shows as 5.8832619E-39 on the 2nd entry into sub1.

Thanks a lot,

Yair

call sub1

call sub2

call sub1

end

subroutine sub1

real, automatic :: x

print *,x

x=1.

end

subroutine sub2

real, automatic :: y(200000)

y=2.

end

0 Kudos
Reply