- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have the following code:
module mesh_mod
implicit none
private
type, public :: ivec_type
integer, allocatable :: iv(:)
end type
public :: jagged_array_3D
contains
function jagged_array_3D(n) result(jag)
implicit none
integer, intent(in) :: n
type(ivec_type), allocatable :: jag(:) ! output
integer :: i
allocate( jag(n) )
do i = 1, n
allocate( jag(i) % iv(i-1 ) )
jag(i) % iv = i
end do
write(*,*) 'exiting jagged_array_3D'
end function
end module
! -------------------------------------------------
program mesh_test
use mesh_mod, only: jagged_array_3D, ivec_type
implicit none
type(ivec_type), allocatable :: jag(:)
integer :: i
write(*,*) 'test #1'
jag = jagged_array_3D(3)
do i = 1, size(jag)
write(*,*) ' jag', i, '|', jag(i) % iv
end do
! test 2
write(*,*) 'test #2'
call test2()
write(*,*) 'testing succesfull'
contains
! =====================================================================
subroutine test2()
use mesh_mod, only: ivec_type, jagged_array_3D
implicit none
type(ivec_type), allocatable :: jag(:)
integer :: i
jag = jagged_array_3D(3)
do i = 1, size(jag)
write(*,*) ' jag', i, '|', jag(i) % iv
end do
deallocate(jag)
end subroutine
end program
It is compiled in debugging mode with the following flags: /nologo /debug:full /Od /heap-arrays1000 /warn:all /Qinit:snan /Qinit:arrays /module:"x64\Debug\\" /object:"x64\Debug\\" /Fd"x64\Debug\vc170.pdb" /traceback /check:all /libs:dll /threads /dbglibs /c
When compiled and run using Intel® Fortran Compiler 2024.0 IFORT I get expected results:
x64\Debug\mesh_test.exe
test #1
exiting jagged_array_3D
jag 1 |
jag 2 | 2
jag 3 | 3 3
test #2
exiting jagged_array_3D
jag 1 |
jag 2 | 2
jag 3 | 3 3
testing succesfull
When compiled and run using Intel® Fortran Compiler 2024.0 IFX I get an error:
x64\Debug\mesh_test.exe
test #1
exiting jagged_array_3D
jag 1 |
jag 2 | 2
jag 3 | 3 3
test #2
exiting jagged_array_3D
forrtl: severe (157): Program Exception - access violation
Image PC Routine Line Source
libifcoremdd.dll 00007FFA37AF06FB Unknown Unknown Unknown
libifcoremdd.dll 00007FFA37AF164F Unknown Unknown Unknown
libifcoremdd.dll 00007FFA37AF0872 Unknown Unknown Unknown
libifcoremdd.dll 00007FFA37AF09D3 Unknown Unknown Unknown
mesh_test.exe 00007FF7DABE2E7C TEST2 57 mesh_test.f90
mesh_test.exe 00007FF7DABE27F0 MESH_TEST 44 mesh_test.f90
mesh_test.exe 00007FF7DABE36EB Unknown Unknown Unknown
mesh_test.exe 00007FF7DABE3BF9 Unknown Unknown Unknown
mesh_test.exe 00007FF7DABE3B1E Unknown Unknown Unknown
mesh_test.exe 00007FF7DABE39DE Unknown Unknown Unknown
mesh_test.exe 00007FF7DABE3C6E Unknown Unknown Unknown
KERNEL32.DLL 00007FFAE4A07374 Unknown Unknown Unknown
ntdll.dll 00007FFAE50BCC91 Unknown Unknown Unknown
Anybody knows why is the code crashing in IFX?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I tested it internally and the issue will be resolved in the next 2025 ifx release.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
do i = 1, n
allocate( jag(i) % iv(i-1 ) )
jag(i) % iv = i
end do
When i==1 you are allocating iv with size 0.
Is it valid to initialize an (allocatable) array with size 0?
What does it mean to "set all values of a null array to x"?
What do you expect to happen when you later attempt to verify if x is in the array?
If the use is valid, then I suggest you add:
do i = 1, n
if(i==1) print *,"Before allocate"
allocate( jag(i) % iv(i-1 ) )
if(i==1) print *,"before assign"
jag(i) % iv = i
end do
This would narrow done if the issue were due to:
/Qinit:snan /Qinit:arrays
or
null array = value
Jim Dempsey
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Jim, Thank you for looking into it. The code is doing what is intended, which is mimic behavior of much larger code which creates jagged array output, with some rows having zero length. According to some Fortran forums modern Fortran is OK with allocating zero size arrays which you can later test with "size" function. I agree that setting zero size array to some value makes little sense and is not something I am doing in the real code, so I changed the loop in my toy example to
do i = 1, n
allocate( jag(i) % iv(i-1 ) )
if (i>1) jag(i) % iv = i
end do
Which made no difference in the running of the code.
I also removed /Qinit:snan /Qinit:arrays compiler options, also without any change in the code performance.
My current options are : /nologo /debug:full /Od /heap-arrays1000 /warn:all /module:"x64\Debug\\" /object:"x64\Debug\\" /Fd"x64\Debug\vc170.pdb" /traceback /check:all /libs:dll /threads /dbglibs /c"
By the way, the crash occurs after call to jagged_array_3D function is done, while it is returning to line 57.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@Jarek_T wrote:By the way, the crash occurs after call to jagged_array_3D function is done, while it is returning to line 57.
It looks like a stack corruption.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>>Fortran is OK with allocating zero size arrays which you can later test with "size" function.
This is correct.
The issue is with the NullArray = Scalar initialization.
Is this defined in the specification?
Is this a NOOP
.or.
Seeing that the Null Array is also Alocatable, would this make the array size(1), iow allocate it to the size of the RHS?
>>By the way, the crash occurs after call to jagged_array_3D function is done, while it is returning to line 57.
From my understanding, result(jag) makes jag intent(out) as indicated by your comment.
Function outputs, in this case an array of UDT, create a temporary object (array of UDT), then on return, copies the temporary array to the LHS allocatable array. Your jag = jagged_array_3D(3) statement.
I recall a similar (same) instance of this problem, and the solution was to use a subroutine
call jagged_array_3D(3, jag)
where the dummy jag is declared as "allocatable, intent(inout)".
I do not recall if the consensus was if the function format was within the standards or not.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>>The issue is with the NullArray = Scalar initialization
I eliminated this operation with performing the assignment only when array is of non-zero size
>>the solution was to use a subroutine
Yes, the code works when using subroutine. However I wanted to understand if I am doing something wrong here or is this a compiler bug.
In the project I am working on, I spend a lot of time analyzing different subroutines of a legacy code trying to figure out what are input and output variables. Once I figure it out I add "intent" statements and if they have a single output I usually convert them to functions, ideally PURE functions, which I find easier to read. This is the first time when there is a difference between function and subroutine, or between IFORT and IFX, and I want to make sure I am not doing something illegal in Fortran.
The issue is only with IFX and I usually do not use IFX as the compiled release code is much slower then the same code compiled with IFORT. However, I run my unit testing in both, just in case.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Initializing an allocatable array with size 0 is valid but results in an empty array; setting all values to `x` has no effect, and verifying if `x` is in the array will always return false. If valid, add a check to handle empty arrays.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The posters original code runs fine with IFX 2024.2.1 on Windows
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I installed IFX 2024.2.1 on my Windows machine and still have the same issue:
Code:
module mesh_mod
implicit none
private
type, public :: ivec_type
integer, allocatable :: iv(:)
end type
public :: jagged_array
contains
function jagged_array(n) result(jag)
implicit none
integer, intent(in) :: n
type(ivec_type), allocatable :: jag(:) ! output
integer :: i
allocate( jag(n) )
do i = 1, n
allocate( jag(i) % iv(i-1 ) )
if (i>1) jag(i) % iv = i
end do
write(*,*) 'exiting jagged_array'
end function
end module
! -------------------------------------------------
program mesh_test
use mesh_mod, only: jagged_array, ivec_type
implicit none
type(ivec_type), allocatable :: jag(:)
integer :: i
write(*,*) 'test #1'
jag = jagged_array(3)
do i = 1, size(jag)
write(*,*) ' jag', i, '|', jag(i) % iv
end do
! test
write(*,*) 'test #2'
call test2()
write(*,*) 'testing succesfull'
contains
! =====================================================================
subroutine test2()
use mesh_mod, only: ivec_type, jagged_array
implicit none
type(ivec_type), allocatable :: jag(:)
integer :: i
jag = jagged_array(3)
do i = 1, size(jag)
write(*,*) ' jag', i, '|', jag(i) % iv
end do
deallocate(jag)
end subroutine
end program
Compiler options:
/nologo /debug:full /Od /heap-arrays1000 /warn:all /module:"x64\Debug\\" /object:"x64\Debug\\" /traceback /check:all /libs:dll /threads /dbglibs /c
Compiling output:
1>------ Build started: Project: mesh_test (IFX), Configuration: Debug x64 ------
Compiling with Intel® Fortran Compiler 2024.2.1 [Intel(R) 64]...
mesh_test.f90
Linking...
Embedding manifest...
Build log written to "file://C:/projects/Das_Boot/mesh_test4/x64/Debug/BuildLog.htm"
mesh_test - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 12:19 PM and took 04.441 seconds ==========
Run output:
x64\Debug\mesh_test.exe
test #1
exiting jagged_array
jag 1 |
jag 2 | 2
jag 3 | 3 3
test #2
exiting jagged_array
forrtl: severe (157): Program Exception - access violation
Image PC Routine Line Source
libifcoremdd.dll 00007FF923C6070C Unknown Unknown Unknown
libifcoremdd.dll 00007FF923C6164F Unknown Unknown Unknown
libifcoremdd.dll 00007FF923C60872 Unknown Unknown Unknown
libifcoremdd.dll 00007FF923C609D3 Unknown Unknown Unknown
mesh_test.exe 00007FF6F0D62EA7 TEST2 57 mesh_test.f90
mesh_test.exe 00007FF6F0D6281F MESH_TEST 44 mesh_test.f90
mesh_test.exe 00007FF6F0D6370B Unknown Unknown Unknown
mesh_test.exe 00007FF6F0D63C29 Unknown Unknown Unknown
mesh_test.exe 00007FF6F0D63B42 Unknown Unknown Unknown
mesh_test.exe 00007FF6F0D639FE Unknown Unknown Unknown
mesh_test.exe 00007FF6F0D63C9E Unknown Unknown Unknown
KERNEL32.DLL 00007FF9458E257D Unknown Unknown Unknown
ntdll.dll 00007FF94792AF28 Unknown Unknown Unknown
Maybe the difference is in compiler options.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Actually removing "/heap-arrays1000" compiler option fixed the issue.
My new compiler options:
/nologo /debug:full /Od /warn:all /module:"x64\Debug\\" /object:"x64\Debug\\" /traceback /check:all /libs:dll /threads /dbglibs /c
My new run output:
x64\Debug\mesh_test.exe
test #1
exiting jagged_array
jag 1 |
jag 2 | 2
jag 3 | 3 3
test #2
exiting jagged_array
jag 1 |
jag 2 | 2
jag 3 | 3 3
testing succesfull
I assume it is some sort of IFX compiler bug. Anybody knows how to report them?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I tested it internally and the issue will be resolved in the next 2025 ifx release.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page