- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi, is the following code standard conforming?
[fortran]integer, allocatable :: array(:) array = [array, 1, 2] array = [array, 3] print *, array end[/fortran]I mean, does the standard allow an unallocated array to be part of an array expression? Compiling with /stand and /assume:realoc_lhs doesn't issue any warning, and there's no runtime error either.
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
With /check:all and 11.1.065 I see an appropriate runtime error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Beyond the scope of your question, if what you wanted to do was to build up the array in pieces, code such as this may be considered:
[fortran]program talloc integer, allocatable :: array(:) if(.not.allocated(array))allocate(array(0)) array = [array, 1, 2] array = [array, 3] print *, array end program talloc [/fortran]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm actually trying to avoid having to check the allocation status every time one entry needs to be added. I already have a subroutine that does just that:
[fortran]subroutine expand(array, idx, istat) integer, allocatable, intent(INOUT) :: array(:) integer, intent(OUT) :: idx, istat integer, allocatable :: aux(:) integer :: sz continue idx = -1 if (ALLOCATED(array)) then sz = SIZE(array) call MOVE_ALLOC(array, aux) allocate (array(1:sz + 1), STAT = istat) if (istat /= 0) then call MOVE_ALLOC(aux, array) return endif array(:sz) = aux sz = sz + 1 deallocate (aux, STAT = istat) else sz = 1 allocate (array(1), STAT = istat) if (istat /= 0) return endif idx = sz end subroutine[/fortran]
That's why I only asked if it was standard conforming. IanH's answer seems to confirm that the code is illegal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
if (allocated(array)) then
sz = size(array) +1 ! new size
allocate(aux(sz), stat=istat)! allocate bigger array
if (istat == 0) then ! ok ?
aux(1:sz-1) = array ! then copy old values tobigger array
aux(sz) = 0 ! initialise new elements
call move_alloc(aux, array)!"rename" aux as array
endif
else
...
endif
only need one move_alloc and don't need a deallocate
Les
sz = size(array) +1 ! new size
allocate(aux(sz), stat=istat)! allocate bigger array
if (istat == 0) then ! ok ?
aux(1:sz-1) = array ! then copy old values tobigger array
aux(sz) = 0 ! initialise new elements
call move_alloc(aux, array)!"rename" aux as array
endif
else
...
endif
only need one move_alloc and don't need a deallocate
Les
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you look carefully, you'll notice that the second MOVE_ALLOC is applied only in case of error (which is highly unlikely). The explicit deallocation of aux is just a precaution (since some compiler flags might affect automatic deallocation).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ah. OK
Les
Les

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