This program demonstrates double deallocation of an array.¶
From ChatGPT 4o:
The given Fortran program performs a series of operations to allocate, deallocate, and print an array. Let’s break it down step by step:
Code Analysis¶
Declarations and Parameters:
nr_values = 5: Defines the number of elements in the array.values: An allocatable integer array that will be dynamically allocated later.i: An integer loop variable.
Allocation:
allocate(values(nr_values), source=[ (i, i=1, nr_values) ])This line allocates an array
valuesof size 5 and initializes its elements with a sequence of integers from 1 to 5 using the implied-do loop(i, i=1, nr_values).
Printing the Array:
print *, valuesThis prints the contents of the
valuesarray to the console. The expected output is:1 2 3 4 5
Deallocate:
deallocate (values)This frees the memory allocated for the
valuesarray.
Double Deallocate:
deallocate (values)This attempts to deallocate the
valuesarray a second time, which is already deallocated.
Behavior of the Code¶
The first deallocation is valid and correctly releases the memory.
The second deallocation of the
valuesarray will likely lead to a runtime error because the arrayvaluesis no longer allocated after the firstdeallocatestatement. Accessing or deallocating an unallocated array is an invalid operation in Fortran.
Expected Outcome¶
When you run this program:
The array will be printed correctly.
The first
deallocatewill work as expected.The second
deallocatewill cause a runtime error such as:Attempting to DEALLOCATE an unallocated variable.
Key Notes¶
Always check the allocation status of allocatable variables using the
allocatedintrinsic before deallocating:if (allocated(values)) deallocate(values)This ensures that the program does not attempt to deallocate an unallocated variable, preventing runtime errors.
Program Code¶
program double_deallocate
implicit none
integer, parameter :: nr_values = 5
integer, dimension(:), allocatable :: values
integer :: i
allocate(values(nr_values), source=[ (i, i=1, nr_values) ])
print *, values
deallocate (values)
deallocate (values)
end program double_deallocate
The above program is compiled and run using Fortran Package Manager (fpm):
Build the Program using FPM (Fortran Package Manager)¶
import os
root_dir = ""
root_dir = os.getcwd()The following FPM configuration file (fpm.toml) was used:
name = "Section_Arrays_Double_Deallocate"
[build]
auto-executables = true
auto-tests = true
auto-examples = true
[install]
library = false
[[executable]]
name="Section_Arrays_Double_Deallocate"
source-dir="app"
main="section_arrays_double_deallocate.f90"
code_dir = root_dir + "/" + "Fortran_Code/Section_Arrays_Double_Deallocate"os.chdir(code_dir)build_status = os.system("fpm build 2>/dev/null")Run the Program using FPM (Fortran Package Manager)¶
exec_status = \
os.system("fpm run") 1 2 3 4 5
Project is up to date
At line 11 of file app/section_arrays_double_deallocate.f90
Fortran runtime error: Attempt to DEALLOCATE unallocated 'values'
Error termination. Backtrace:
#0 0x7f536dc218c2 in ???
#1 0x7f536dc223b9 in ???
#2 0x7f536dc22949 in ???
#3 0x561b9535074a in double_deallocate
at app/section_arrays_double_deallocate.f90:11
#4 0x561b953507a4 in main
at app/section_arrays_double_deallocate.f90:12
<ERROR> Execution for object " Section_Arrays_Double_Deallocate " returned exit code 2
<ERROR> *cmd_run*:stopping due to failed executions
STOP 2