Automatic array allocation in Fortran 95?

In summary, the conversation discusses the use of automatic allocation in Fortran 95, specifically in regards to a variable sized array. The code appears to run successfully without an explicit "allocate" command, leading to the question of whether it is a legitimate Fortran 95 syntax or specific to the compiler being used. The response is that it is legal and referred to as Automatic Object.
  • #1
uart
Science Advisor
2,795
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
  • #2
It's legal, it is called Automatic Object.

...gotta run.
 
  • #3
Thanks gsal :smile:
 

1. What is automatic array allocation in Fortran 95?

Automatic array allocation in Fortran 95 is a feature that allows arrays to be dynamically allocated at run-time. This means that the size of the array does not need to be specified in the code, but can be determined during program execution.

2. How is automatic array allocation different from static array allocation?

Static array allocation requires the size of the array to be known and specified in the code before the program is compiled. This means that the size of the array cannot change during program execution. Automatic array allocation, on the other hand, allows for arrays to be dynamically allocated and resized during run-time.

3. How is automatic array allocation implemented in Fortran 95?

In Fortran 95, automatic array allocation is achieved through the use of the ALLOCATE statement. This statement allows for arrays to be allocated and deallocated dynamically during program execution.

4. What are the advantages of using automatic array allocation?

One of the main advantages of automatic array allocation is that it allows for more flexible and efficient use of memory. It also simplifies the coding process, as the size of the array does not need to be specified beforehand. Additionally, it can help to avoid potential errors that may arise from fixed-size arrays.

5. Are there any limitations to using automatic array allocation in Fortran 95?

Yes, there are some limitations to using automatic array allocation in Fortran 95. For example, it is not suitable for very large arrays or for arrays that require frequent resizing. Additionally, it may not be supported by all Fortran compilers.

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
4
Views
588
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
7K
  • Programming and Computer Science
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top