Automatic array allocation in Fortran 95?

Click For Summary
SUMMARY

The discussion confirms that the code snippet provided is valid Fortran 95 syntax, specifically utilizing automatic allocation for variable-sized arrays. The g95 compiler supports this feature, allowing arrays to be allocated on the stack at runtime without an explicit "allocate" command. This behavior is referred to as "Automatic Object" allocation in Fortran 95, enabling dynamic array sizing based on runtime parameters.

PREREQUISITES
  • Understanding of Fortran 95 syntax
  • Familiarity with the g95 compiler
  • Knowledge of array allocation concepts in programming
  • Basic principles of subroutines in Fortran
NEXT STEPS
  • Research "Fortran 95 Automatic Object allocation"
  • Explore the g95 compiler documentation for advanced features
  • Learn about dynamic memory management in Fortran
  • Investigate differences between stack and heap allocation in programming
USEFUL FOR

Fortran developers, educators teaching Fortran programming, and anyone interested in dynamic array management in Fortran 95.

uart
Science Advisor
Messages
2,797
Reaction score
21
The following code seems to run ok under fortran 95 (g95 compiler), but I was unsure about the exact nature of the array assignment. As you can see it is a variable sized array, without any explicit "allocate" command. The code works even when the array size could not be known at compile time, so I'm guessing that it's automatically allocated on the stack at run time. Just wondering if this legitimate Fortran 95 syntax (or something compiler specific)?

Code:
 subroutine test(n)
 integer :: n
 implicit none

   integer :: a(n)     ! Is this automatic allocation?
   integer :: k
   do k=1,n
      a(k)=k
   end do
   print *,a

 end subroutine test
 
Technology news on Phys.org
It's legal, it is called Automatic Object.

...gotta run.
 
Thanks gsal :smile:
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K