Fortran Automatic array allocation in Fortran 95?

Click For Summary
The discussion centers on the use of variable-sized arrays in Fortran 95, specifically regarding the syntax and behavior of array allocation without an explicit "allocate" command. The code snippet provided demonstrates a subroutine that defines an integer array 'a' with a size determined at runtime, which raises questions about whether this is legitimate Fortran 95 syntax or specific to certain compilers. It is confirmed that this practice is legal in Fortran 95 and is referred to as "Automatic Object" allocation, indicating that the array is automatically allocated on the stack during execution. The conversation highlights the flexibility of Fortran 95 in handling dynamic array sizes.
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:
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

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
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K