- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I found out that ifort (ifx as well) enable the following code to run successfully:
module test
implicit none
logical, allocatable :: array(:)
end module
program main
use test
implicit none
logical :: x
x = .false.
array(:) = x
print *, 'hello'
end program
While GNU gfortran will segfault since array has not been allocated. Which seems to me to be the right behavior as it enables catching a bug in the code.
I tried using the flags "-check all -warn all" but the code still runs prompting "hello".
Is there something else that I could use to force the compiler to catch that I'm doing an assignment on an uninitialized array?
Here a compiler explorer link with this minimal code: https://godbolt.org/z/4MdEc8do5
Thanks,
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi. My first thought is to check that is allocated before using it, e.g.
module m_test
implicit none
logical, allocatable :: array(:)
end module m_test
!
program test
use m_test
implicit none
logical :: x
x = .false.
if (allocated(array)) then
array (:) = x
else
error stop "error: array not allocated"
end if
print *, 'hello'
end program test
Then,
$ ifx -O2 -error-limit 4 -e03 -fimf-arch-consistency=true -fimf-precision=high -finline-functions -fp-model consistent -fpp -fpe-all=1 -ipo -mavx -mtune=generic -m64 -parallel-source-info=1 -qmkl -qopenmp -qopt-report=3 -standard-semantics -std18 -traceback -WB -warn all -xHost -o testcase-0140-ifx.exe testcase-0140.f90
$ ./testcase-0140-ifx.exe
error: array not allocated
I don't know if there is another (portable) way to do it.
Greetings. Jorge.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Both ifort and ifx default standard_realloc_lhs, earler versions did not, apparently gfortran does not (it may be using -nostandard_realloc_lhs).
See option standard_realloc_lhs.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @jimdempseyatthecove, thanks for the clue. Tried using "-nostandard_realloc_lhs" on compiler explorer but I see no difference:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @jdelia ,
Yes, I use if(allocated( )) at initialization of the program. The thing is that the memory allocation+initialization procedure and the actual assignment/use are in separate procedures. I found a place in the code in which it would be possible to do an assignment having inadvertently bypassed the allocation. Thus I was hoping to have an option that would lead to an error like a segfault to detect that a non-allocated array is being used an fix the bug, instead of having the program running.
Cheers,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In the example above the assignment is to an array section, not an allocatable array. Then automatic allocation does not occur, as the left-hand side is not allocatable:
array(:) = x
Note also that the right-hand side is a scalar, not an array. I am not sure what the size of the array would become if you do:
array = x
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Arjen,
I'm not looking for automatic allocation, and here I'm just giving a constant value to the whole array.
(this "array = x" has the same behavior as "array(:) = x" for this specific example, that's just fine)
The ideal behavior I'm looking for is a runtime error that would catch the issue just like with gfortran that segfaults.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ah, but the two are NOT equivalent:
- With "array = x", the left-hand side is an allocatable object and the rules of automatic reallocation force it to become allocated to a convenient size.
- With "array(:) = x", the left-hand side is not allocatable, so it should have a definite size, equal to the right-hand side, unless the right-hand side is a scalar.
Here is where gfortran and Intel Fortran differ:
- gfortran causes a run-time failure.
- Intel Fortran allocates it to zero size.
I would expect the latter behaviour to be conforming to the standard.
Still, the example program is different and should result in a run-time failure.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
/check:pointer should have caught this, but it doesn't. I wondered if the compiler may have thrown away the assignment because it was never used, but that's not the case either. I consider the lack of an error from /check:pointer a bug.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @Steve_Lionel that is exactly what I thought as I also tried using that flag and didn't help! thanks for the confirmation!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I note that the debugger shows the array as unallocated/undefined. An allocatable array's allocation status is ALWAYS defined, unlike a pointer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Indeed, the debugger shows the array as undefined.
So, as of now it is only possible to catch the bug by doing:
if(allocated(array))then
...
else
!> some error + stop
end if
and putting a break point inside the else. But if someone forgets to put that safeguard, we cannot rely on attaching the debugger to see where it crashes, as it will crash somewhere else, completely unrelated to the actual problem (this is the situation that brough me here).
I would insist on this:
@Steve_Lionel wrote:(...) I consider the lack of an error from /check:pointer a bug.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The array was not allocated... but no error was reported.
Now the following is interesting
An unallocated array w/o/(:) assigned to scalar remains unallocated (no error)
Assigned to array, behaves as expected:
Jim Dempsey
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page